No module named when using PyInstaller
Solution 1
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.
Solution 2
Had a similar problem with no module named FileDialog
. Discovered that with version 3.2, I could use
pyinstaller --hidden-import FileDialog ...
instead of modifying my main script.
Solution 3
Pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in A will not be seen.
There is no need to change anything in your python scripts. You can directly add the missing imports to the spec file. Just change the following line:
hiddenimports=[],
to
hiddenimports=["Tkinter", "FileDialog"],
Solution 4
If you are getting ModuleNotFoundError: No module named ...
errors and you:
- call PyInstaller from a directory other than your main script's directory
- use relative imports in your script
then your executable can have trouble finding the relative imports.
This can be fixed by:
-
calling PyInstaller from the same directory as your main script
-
OR removing any
__init__.py
files (empty__init__.py
files are not required in Python 3.3+) -
OR using PyInstaller's
paths
flag to specify a path to search for imports. E.g. if you are calling PyInstaller from a parent folder to your main script, and your script lives insubfolder
, then call PyInstaller as such:pyinstaller --paths=subfolder subfolder/script.py
.
Solution 5
I was facing the same problem and the following solution worked for me:
- I first removed the virtual environment in which I was working.
- Reinstalled all the modules using pip (note: this time I did not create any virtual environment).
- Then I called the pyinstaller.
- The .exe file created thereafter executed smoothly, without any module import error.
a_guest
Updated on January 04, 2022Comments
-
a_guest over 2 years
I try to compile a Python project under Windows 7 using PyInstaller. The project works fine, there are no issues, however when I try to compile it the result doesn't work. Though I get no warnings during compilation there are many in the
warnmain.txt
file in thebuild
directory: warnmain.txtI don't really understand those warnings, for example "no module named numpy.pi" since
numpy.pi
is no module but a number. I never tried to importnumpy.pi
. I did importnumpy
andmatplotlib
explicitly. In addition I'm using PyQt4. I thought the error might be related to those libraries.However I was able to compile a simple script which uses numpy succesfully:
import sys from PyQt4 import QtGui, QtCore import numpy as np class MainWindow(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) self.pb = QtGui.QPushButton(str(np.pi), self) app = QtGui.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())
Successfully here means that the created executable file actually showed the desired output. However there is also a
warnmain.txt
file created which contains exactly the same 'warnings' as the one before. So I guess the fact that compiling my actual project does not give any success is not (or at least not only) related to those warnings. But what else could be the error then? The only output during compilation are 'INFO's and none of the is a negative statement.I did not specify an additional hook directory but the hooks where down using the default directory as far as I could read from the compile output, e.g.
hook-matplotlib
was executed. I could not see any hook fornumpy
neither could I for my small example script but this one worked. I used the following imports in my files (not all in the same but in different ones):import numpy as np import matplotlib.pyplot as ppl from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar from PyQt4 import QtGui, QtCore import json import sys import numpy # added this one later import matplotlib # added this one later
Since PyInstaller does not give any errors/warnings I could not figure out if the problem is related to the libraries or if there is something else to be considered.
-
panofish almost 9 yearsThis is the redirect command I used: myprogram.exe 1> errors.txt 2>&1 --- I got the same result as you... I was missing Tkinter and FileDialog modules as well :) Thanks, much appreciated.
-
Efren about 8 yearsSo is the problem that PyInstaller doesn't pick up second level imports? Or was this not a project with a setup.py?
-
a_guest about 8 yearsYes. If you import a module and this module imports other modules itself then PyInstaller apparently won't realize those second level imports.
-
niCk cAMel over 3 yearsWow, this helped, tnx. Though I had to make my main script folder a python package by creating
__init__.py
in the same folder. Otherwise strange things would happen depending on from where the executable was run. Anything from failing relative import to not succeeding importingserial
. -
Sean McCarthy about 3 yearsSimilar to you, I found pyinstaller could not find modules I installed with Poetry. I could, however, use modules installed in a "venv" virtual environment created with
python3 -m venv venv
-
Ankit Chawla about 3 yearsThis one worked for me Thanks a lot for this.
-
LarsH over 2 yearsI've had to use this
--hidden-import
option more than once, to fix ModuleNotFound errors that appeared when trying to run the compiled exe. The missing modules were_cffi_backend
andnacl
, when using thenacl
module. -
nathancy over 2 yearsHey thanks for this answer, especially the
--paths
section. THANK YOU -
n1nsa1d00 about 2 yearsYou are a lifesaver!!!