Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

anaconda - PyInstaller win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

While using PyInstaller (dev version 4.0.dev0+8196c57ab), produced an OSError: [WinError 2] 'The system cannot find the file specified.'

  • Python version: 3.7
  • GUI: PySide2 5.13.0
  • Other packages: lxml, BeautifulSoup, Matplotlib (with dependencies like numpy), Pandas, pypiwin32, reportlab, Theano
  • Anaconda 3, Windows
  • Compiled with PyInstaller --onedir

What I've tried:

  • Tried downgrading PyInstaller to 2.1 as suggested here: https://github.com/pyinstaller/pyinstaller/issues/3916 but doesn't work as Python 3 is not supported
  • Was originally on PyInstaller 3.5, upgraded to dev version as suggested under some of the issues on PyInstaller github
  • Tried including --exclude-module=.git
import PyInstaller.__main__

if __name__ == '__main__':
    PyInstaller.__main__.run([
        '--name=%s' % 'Dummy App',
        '--onedir',
        '--nowindowed',
        r'--workpath=C:UsersUser1Desktopuild7',
        r'--distpath=C:UsersUser1Desktopdist7',
        '--hidden-import=theano.tensor.shared_randomstreams',
        '--hidden-import=pandas._libs.tslibs.timedeltas',
        '--clean',
        '--add-data={0};.'.format('redacted.xml'),
        '--add-data={0};{0}'.format('redacted_folder'),
        '--add-data={0};.'.format('redacted.pdf'),
        '--exclude-module={0}'.format('.git'),
        '--log-level=WARN',
        'MainWindow.py'
    ])

Full stacktrace:

79702 DEBUG: Analyzing .gitobjects78e83411cea88cd038acb12c005a984fc0d6d423
Traceback (most recent call last):
  File "C:UsersUser1Anaconda3envsids_guilibsite-packageswin32ctypespywin32pywintypes.py", line 35, in pywin32error
    yield
  File "C:UsersUser1Anaconda3envsids_guilibsite-packageswin32ctypespywin32win32api.py", line 43, in LoadLibraryEx
    return _dll._LoadLibraryEx(fileName, 0, flags)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packageswin32ctypescorectypes\_util.py", line 42, in check_null
    raise make_error(function, function_name)
OSError: [WinError 2] The system cannot find the file specified.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/User1/Dropbox/GitHub_Repos/DiabetesReportGenerator_v2/pyinstaller_freeze.py", line 37, in <module>
    'MainWindow.py'
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstaller\__main__.py", line 112, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstalleruildinguild_main.py", line 732, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstalleruildinguild_main.py", line 679, in build
    exec(code, spec_namespace)
  File "C:UsersUser1DropboxGitHub_ReposDiabetesReportGenerator_v2Risk Calculator.spec", line 17, in <module>
    noarchive=False)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstalleruildinguild_main.py", line 242, in __init__
    self.__postinit__()
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstalleruildingdatastruct.py", line 158, in __postinit__
    self.assemble()
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstalleruildinguild_main.py", line 468, in assemble
    redirects=self.binding_redirects))
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstallerdependindepend.py", line 226, in Dependencies
    for ftocnm, fn in getAssemblyFiles(pth, manifest, redirects):
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstallerdependindepend.py", line 402, in getAssemblyFiles
    for assembly in getAssemblies(pth):
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstallerdependindepend.py", line 353, in getAssemblies
    res = GetManifestResources(pth)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstallerutilswin32winmanifest.py", line 1005, in GetManifestResources
    return winresource.GetResources(filename, [RT_MANIFEST], names, languages)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packagesPyInstallerutilswin32winresource.py", line 168, in GetResources
    hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packageswin32ctypespywin32win32api.py", line 43, in LoadLibraryEx
    return _dll._LoadLibraryEx(fileName, 0, flags)
  File "C:UsersUser1Anaconda3envsids_guilibcontextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:UsersUser1Anaconda3envsids_guilibsite-packageswin32ctypespywin32pywintypes.py", line 37, in pywin32error
    raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')

P.S. Compilation and the exe functioned perfectly with virtualenv environment created from Anaconda environment earlier (without need for --exclude-module=.git), but was too bloated with packages because the virtualenv environment is still considered to be part of the conda environment. So I used a new conda environment, and this error appeared

I've gotten it to work by creating another virtualenv environment in this new conda environment, but that is a very roundabout way. Is there any workaround?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For me, PyInstaller seems to search through .git folder and redacted_folder unnecessarily

I solved this problem by:

  1. MOVING the folder(s) out of the directory before freezing

  2. Additionally, if the folder is required for the frozen app to run (e.g. as a data folder), I would also either:

    • Comment / remove the corresponding part for --add-data and manually copy the folder over, or

    • Change the path for --add-data e.g. if you moved redacted_folder to your Desktop, you can change to '--add-data=C:/Users/User1/Desktop/redacted_folder'

You'll still need .git and redacted_folder back in the original directory to use Git and run your code without freezing


For example, I had trouble with .git and redacted_folder. The frozen executable needed redacted_folder to run parts of the code.

What I did was:

  1. Move .git and redacted_folder to a separate directory

  2. Then, I commented out the line to add data for redacted_folder:

import PyInstaller.__main__

if __name__ == '__main__':
    PyInstaller.__main__.run([
        '--name=%s' % 'Dummy App',
        '--onedir',
        '--nowindowed',
        r'--workpath=C:UsersUser1Desktopuild7',
        r'--distpath=C:UsersUser1Desktopdist7',
        '--hidden-import=theano.tensor.shared_randomstreams',
        '--hidden-import=pandas._libs.tslibs.timedeltas',
        '--clean',
        '--add-data={0};.'.format('redacted.xml'),
        # Remove `redacted_folder` and comment out the following line
        # '--add-data={0};{0}'.format('redacted_folder'),
        '--add-data={0};.'.format('redacted.pdf'),
        '--exclude-module={0}'.format('.git'),
        '--log-level=WARN',
        'MainWindow.py'
    ])
  1. After freezing, I copied redacted_folder over to the appropriate location relative to the frozen executable in distpath

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...