pyinstaller reported an error NotADirectoryError: [WinError 267] The directory is invalid

pyinstaller reported an error NotADirectoryError: [WinError 267] The directory is invalid

The most recently used pyinstallerpackaging script is a windows application, and no error was reported during packaging; the following error occurred when double-clicking to execute the executable file:

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

Afterwards, I browsed the Internet and found no useful method, but finally found the cause of the error.

analysis

The above error appears on line 61, and its content is:

	file_list = os.listdir(path)

Indicates to list the files and folders under the directory path;

I always thought it was a problem with pyinstaller, and you can find a related [2564] Failed to execute scriptsolution, pyinstaller failed to execute script .

Most of them are explaining the need to import the relevant library path.

Later, I felt that I should look carefully at the place where the error was reported. It should be the error of line 61. Finally I saw relevant discussions on StackOverflow.

solve

Original discussion URL: https://stackoverflow.com/questions/11062466/py2exe-windowserror-error-267-the-directory-name-is-invalid

The content title is: py2exe: WindowsError: [Error 267] The directory name is invalid

It can be seen that a similar phenomenon appears in py2exe.

The errors mentioned in the article are:

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/*.*'

The content of Line 240-246 of program.py is:

 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))

The original answer is as follows:

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

Translated to:

You try to use the sys.pathcontents of the list of directories. From the manual, it seems:

sys.pathA list of strings, specifying the search path of the module. Initialized from the environment variable PYTHONPATH, plus the default value related to the installation.

After the program is initialized at startup, the first item in the list, path [0], is the directory containing the script used to call the Python interpreter. If the script directory is not available (for example, to call the interpreter interactively or read scripts from standard input), path[0] is an empty string that will direct Python to search for modules in the current directory first. Note that as a result of PYTHONPATH, the script directory was inserted before the entry.

For py2exe executable files like yours, sys.path is a list containing the path to library.zip (this file holds all the pure source modules found by py2exe in your installation. These files may require your executable Files to work).

But you cannot use the zip archive file as the path to 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/*.*'
>>> 

Maybe you are not looking for sys.path, but for the "current directory" indicated by the variable name. If this is the case, it os.getcwd()will complete this work.

modify

It is true that there is an operation to get the current directory in the script, and what is used is sys.path[0], os.getcwd()after modifying it, proceed normally pyinstaller, and the executable file runs successfully.

to sum up

sys.pathIs a list sys.path[0]of directories used to call the interpreter. The packaged executable file will not call the interpreter's directory.

Use os.getcwd()Done to get the current directory.

2021-03-17

Guess you like

Origin blog.csdn.net/sinat_31206523/article/details/114948998