Module Not Found After Building Python Project by Using Pysinstaller

No module named when using PyInstaller

The problem were some runtime dependencies of matplotlib. So the compiling was fine while running the program threw some errors. Because the terminal closed itself immediately I didn't realize that. After redirecting stdout and stderr to a file I could see that I missed the libraries Tkinter and FileDialog. Adding two imports at the top of the main solved this problem.

Module not found after building python project by using pysinstaller

matplotlib requires a config file names matplotlibrc to be found in one of 4 specific locations, the first one being the courant directory (see https://matplotlib.org/tutorials/introductory/customizing.html#the-matplotlibrc-file) One this file prepared as per need it can be embeeded in the exe by adding --add-data=matplotlibrc;. to the build command (replace ; with : for non-Windows systems)

How to resolve Pyinstaller module not found error

I resolved the issue by loading the needed packages and modules as data.

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['startup.py'],
pathex=['/home/kenneth/PycharmProjects/universal_predictor'],
binaries=[],
datas=[('.streamlit', '.streamlit'), ('.data', '.data'), ('models', 'models'), ('stapp', 'stapp'), ('utils.py', '.')],
hiddenimports=[],
hookspath=['.'],
runtime_hooks=[],
excludes=['torch.distributions'],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
[],
name='startup',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
upx_exclude=[],
runtime_tmpdir=None,
console=False , icon='unipredictor-icon.ico')

ModuleNotFound Pyinstaller

The short answer is that you're telling PyInstaller the wrong directory; you want to use -p /path/to/Project instead. This is because your DoSomething folder is inside the root Project directory. You're telling PyInstaller where to look for modules/packages that you try to import.


For a little more context...

I don't use PyCharm myself, but it would appear that it's handling something for you automatically: it's adding your top-level Project path to your Python Path. This means that when your code attempts to import a module, Project is one of the places it looks for that module name. This is the reason your code works as-is in PyCharm.

If you open a standard terminal, go to Project, and run python GUIs/GUI_main.py, you should find that you get an ModuleNotFoundError. In order to make it work, you can add the proper directory to the Python Path environment variable (for the current session) with:

export PYTHONPATH=/path/to/Project:$PYTHONPATH

After doing this, running the script directly should work. What's also neat is that PyInstaller respects your Python Path... so you can then run PyInstaller, without specifying any search folders, and it will correctly find your other Python file.


Sidenote: standard practice is to keep Python package/module (essentially folder/file) names all lowercase.



Related Topics



Leave a reply



Submit