pyinstaller 报错NotADirectoryError:[WinError 267]目录无效

pyinstaller 报错NotADirectoryError:[WinError 267]目录无效

最近使用pyinstaller打包脚本为windows应用程序,打包期间未报错;双击执行可执行文件的时候出现如下报错:

Traceback (most recent call last):
  File "auto_label.py", line 170, in <module>
  File "auto_label.py", line 61, in get_pdf_list
NotADirectoryError: [WinError 267] 目录名称无效。: 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\_MEI90842\\base_library.zip'
[2564] Failed to execute script auto_label

随之,在网上一顿翻阅,没有发现有用的方法,但最后还是找到了出错的原因。

分析

上述报错出现在line 61,其内容为:

	file_list = os.listdir(path)

表示列举出目录path下的文件及文件夹;

一直以为是pyinstaller的问题,可以找到相关的[2564] Failed to execute script的解决方案,pyinstaller failed to execute script

大多数是在说明需要导入相关的库路径。

后来觉得还是应该从报错的地方仔细看,应该是line 61的报错问题。最终在StackOverflow上看到了相关讨论。

解决

原讨论网址:https://stackoverflow.com/questions/11062466/py2exe-windowserror-error-267-the-directory-name-is-invalid

内容标题是:py2exe: WindowsError: [Error 267] The directory name is invalid

可以看出在py2exe中也有类似的现象出现。

文中提到的错误为:

Traceback (most recent call last):
File "program.py", line 427, in <module>
File "program.py", line 242, in __init__
WindowsError: [Error 267] The directory name is invalid: 'C:\\Users\\Bob\applications\\Program\\test\\v0.6\\dist\\library.zip/*.*'

程序program.py的Line 240-246内容为:

 file_list = []
 root_dir = sys.path[0]
 for path in os.listdir(root_dir):
    full_path = os.path.join(root_dir, path).lower()
    if os.path.isfile(full_path) and full_path.endswith('txt'):
        # create list of (filename, dir) tuples
        file_list.append((path.lower(), full_path))

原回答如下:

You are trying to list the items in sys.path(). From docs:

sys.path A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

In the case of a py2exe executable like yours, sys.path is a list containing the path for library.zip (the archive that holds all the pure source modules py2exe find in your installation that could be needed for your executable to work).
But you can not use a zip archive for the path for os.listdir

import os
d = ‘C:\test.zip’
os.listdir(d)
Traceback (most recent call last):
File “”, line 1, in
WindowsError: [Error 267] El nombre del directorio no es válido: ‘C:\test.zip/.

Probably you are not looking for sys.path but for the “current dir” as the name of your variable indicates.
If this is the case, then os.getcwd will do the job

翻译为:

你尝试使用sys.path列举目录中的内容,从手册里面看来:

sys.path字符串列表,指定模块的搜索路径。 从环境变量PYTHONPATH初始化,再加上与安装有关的默认值。

在程序启动时初始化后,该列表的第一项path [0]是包含用于调用Python解释器的脚本的目录。 如果脚本目录不可用(例如,以交互方式调用解释器或从标准输入中读取脚本),则path [0]为空字符串,该字符串将引导Python首先搜索当前目录中的模块。 请注意,由于PYTHONPATH的结果,在插入条目之前插入了脚本目录。

对于像您这样的py2exe可执行文件,sys.path是一个列表,其中包含library.zip的路径(该档案保存了py2exe在您的安装中找到的所有纯源模块,这些文件可能需要您的可执行文件才能工作)。

但是您不能将zip归档文件用作os.listdir的路径。

>>> import os
>>> d = 'C:\\test.zip'
>>> os.listdir(d)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
WindowsError: [Error 267] El nombre del directorio no es válido: 'C:\\test.zip/*.*'
>>> 

可能您不是在寻找sys.path,而是寻找变量名称所指示的“当前目录”。 如果是这种情况,则os.getcwd()将完成此工作。

修改

确实脚本里面有获取当前目录的操作,而且使用的是sys.path[0] ,修改为os.getcwd()后,正常再进行pyinstaller,可执行文件运行成功。

总结

sys.path是一个列表,sys.path[0] 用于调用解释器的目录。打包后的可执行文件不会调用解释器的目录。

os.getcwd()完成获取当前目录。

2021-03-17

猜你喜欢

转载自blog.csdn.net/sinat_31206523/article/details/114948998