Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python 2.7 - PyInstaller runs fine but exe file errors: No module named, Failed to Execute Script

I am running the following code:

pyinstaller --onefile main.py

main.py looks like:

import sys
import os
sys.path.append(r'C:ModelUtilities')
from import_pythonpkg import *

......

import_pythonpkg.py looks like:

from astroML.density_estimation import EmpiricalDistribution
import calendar
import collections
from collections import Counter, OrderedDict, defaultdict
import csv

....

By running the pyinstaller on main.py, main.exe file is created successfully.

But when I run main.exe it gives error with astroML. If I move astroML to main.py from import_pythonpkg.py, there is no error with astroML. Now I get error with csv.

i.e. if I change my main.py to look as:

import sys
from astroML.density_estimation import EmpiricalDistribution
import os
sys.path.append(r'C:ModelUtilities')
from import_pythonpkg import *

......

The astroML error is no longer present when I run main.exe.

There is no error with import calendar line in import_pythonpkg.py at all.

I am not sure how to handle this random error with packages when running main.exe after pyinstaller run.

import_pythonpkg is located at r'C:ModelUtilities'

Edit:

Error with main.exe looks as following even though the original main.py runs fine. Pyinstaller was even able to let me create the main.exe without error.

    Traceback (most recent call last):
  File "main.py", line 8, in <module>
  File "C:ModelUtilitiesimport_pythonpkg.py", line 1, in <module>
    from astroML.density_estimation import EmpiricalDistribution
ImportError: No module named astroML.density_estimation
[29180] Failed to execute script main
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

I believe PyInstaller is not seeing import_pythonpkg. In my experience, when adding to the path or dealing with external modules and dlls, PyInstaller will not go searching for that, you have to explicitly tell it to do so. It will compile down to an .exe properly because it just ignores it, but then won't run. Check to see if there are any warnings about missing packages or modules when you run your PyInstaller command.

But how to fix it...If indeed this is the issue (which I am not sure that it is) you can try 3 things:

1) move that package into your working directory and avoid using sys.path.append. Then compile with PyInstaller to so see if this works, then you know the issue is that pyinstaller is failing to find import_pythonpkg. You can stop there if this works.

2) explicitly tell PyInstaller to look there. You can use the hidden-import tag when compiling with PyInstaller to let it know (give it the full pathname).

--hidden-import=modulename

for more info, check here: How to properly create a pyinstaller hook, or maybe hidden import?

3) If you use the spec file that PyInstaller creates, you can try adding a variable call pathex to tell PyInstaller to search there for things:

block_cipher = None
a = Analysis(['minimal.py'],
    pathex=['C:\Program Files (x86)\Windows Kits\10\example_directory'],
    binaries=None,
    datas=None,
    hiddenimports=['path_to_import', 'path_to_second_import'],
    hookspath=None,
    runtime_hooks=None,
    excludes=None,
    cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
    cipher=block_cipher)
exe = EXE(pyz,... )
coll = COLLECT(...)

for more information on spec files: https://pyinstaller.readthedocs.io/en/stable/spec-files.html

(notice you can also add hiddenimports here)

This answer may also prove helpful: PyInstaller - no module named


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...