6

I am running the following code:

pyinstaller --onefile main.py

main.py looks like:

import sys
import os
sys.path.append(r'C:\Model\Utilities')
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:\Model\Utilities')
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:\Model\Utilities'

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:\Model\Utilities\import_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
The4thIceman
  • 3,709
  • 2
  • 29
  • 36
Zanam
  • 4,607
  • 13
  • 67
  • 143
  • do you have the exact error message? – The4thIceman Oct 21 '17 at 08:56
  • pyinstaller may have run without error, but it may not be including the proper stuff. Are there any warnings? you can also post the log of the pyinstaller command so we have a full picture of what is happening. – The4thIceman Oct 23 '17 at 16:02

2 Answers2

1

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

The4thIceman
  • 3,709
  • 2
  • 29
  • 36
1

It is about to module which loaeded on your computer. If your IDE is different from your environment, you have to load same modules on your device via pip. Check the modules on CMD screen and complete the missing modules.

Sometimes you must load the modules all IDEs on your device. In my case, there were two IDEs (pycharm and anaconda). I used pycharm but pyinstaller used anaconda's modules so i unistalled anaconda and tried again. now it works..

Irfan
  • 21
  • 1