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 import
s 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
Fastest Way to Download 3 Million Objects from a S3 Bucket
List Comprehension Rebinds Names Even After Scope of Comprehension. Is This Right
Psycopg2: Insert Multiple Rows with One Query
Split String on Whitespace in Python
Why Is Button Parameter "Command" Executed When Declared
Permutations Between Two Lists of Unequal Length
Typeerror: 'Int' Object Is Not Callable
Stripping Everything But Alphanumeric Chars from a String in Python
Tkinter.Tclerror: Image "Pyimage3" Doesn't Exist
No Such File or Directory "Limits.H" When Installing Pillow on Alpine Linux
How to Print a Variable Name in Python
Getting an "Invalid Syntax" When Trying to Perform String Interpolation
Dynamically Evaluate an Expression from a Formula in Pandas
How to Run Functions in Parallel
Pip Install MySQL-Python Fails with Environmenterror: MySQL_Config Not Found