Python 使用 PyInstaller 时没有命名模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25733467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No module named when using PyInstaller
提问by a_guest
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.txtfile in the builddirectory: warnmain.txt
我尝试使用 PyInstaller 在 Windows 7 下编译 Python 项目。该项目运行良好,没有问题,但是当我尝试编译它时,结果不起作用。虽然我在编译过程中没有收到警告warnmain.txt,但build目录中的文件中有很多:warnmain.txt
I don't really understand those warnings, for example "no module named numpy.pi" since numpy.piis no module but a number. I never tried to import numpy.pi. I did import numpyand matplotlibexplicitly. In addition I'm using PyQt4. I thought the error might be related to those libraries.
我不太明白这些警告,例如“没有名为 numpy.pi 的模块”,因为numpy.pi它不是模块而是一个数字。我从未尝试导入numpy.pi. 我确实导入numpy并matplotlib明确。另外我正在使用 PyQt4。我认为错误可能与这些库有关。
However I was able to compile a simple script which uses numpy succesfully:
但是我能够编译一个简单的脚本,它成功地使用了 numpy:
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.txtfile 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.
在这里成功意味着创建的可执行文件实际上显示了所需的输出。然而,还warnmain.txt创建了一个文件,其中包含与之前完全相同的“警告”。所以我想编译我的实际项目没有取得任何成功的事实与(或至少不仅)与这些警告无关。但是还有什么可能是错误呢?编译期间唯一的输出是'INFO's,并且没有一个是否定语句。
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-matplotlibwas executed. I could not see any hook for numpyneither 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):
我没有指定额外的钩子目录,而是使用默认目录下的钩子,就我可以从编译输出中读取而言,例如hook-matplotlib被执行。我看不到任何钩子,numpy因为我的小示例脚本也看不到任何钩子,但是这个有效。我在我的文件中使用了以下导入(并非全部相同但在不同的文件中):
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.
由于 PyInstaller 没有给出任何错误/警告,我无法确定问题是否与库有关,或者是否还有其他需要考虑的问题。
采纳答案by a_guest
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 stdoutand stderrto a file I could see that I missed the libraries Tkinterand FileDialog. Adding two imports at the top of the main solved this problem.
问题是 matplotlib 的一些运行时依赖项。所以在运行程序时编译很好,抛出了一些错误。因为终端立即关闭了我没有意识到这一点。重定向后stdout,并stderr到一个文件,我可以看到,我错过了图书馆Tkinter和FileDialog。import在 main 的顶部添加两个s 解决了这个问题。
回答by fivef
I had the same problem with pyinstaller 3.0 and weblib. Importing it in the main didn't help.
我在 pyinstaller 3.0 和weblib. 在主要导入它没有帮助。
Upgrading to 3.1 and deleting all build files helped.
升级到 3.1 并删除所有构建文件有帮助。
pip install --upgrade pyinstaller
回答by HHest
Had a similar problem with no module named FileDialog. Discovered that with version 3.2, I could use
有类似的问题no module named FileDialog。发现使用 3.2 版,我可以使用
pyinstaller --hidden-import FileDialog ...
pyinstaller --hidden-import FileDialog ...
instead of modifying my main script.
而不是修改我的主脚本。
回答by user1251007
Pyinstaller won't see second level imports. So if you import module A, pyinstaller sees this. But any additional module that is imported in Awill not be seen.
Pyinstaller 不会看到二级导入。因此,如果您导入模块A, pyinstaller 会看到这一点。但是在A 中导入的任何附加模块都不会被看到。
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:
无需更改 Python 脚本中的任何内容。您可以直接将缺少的导入添加到规范文件中。只需更改以下行:
hiddenimports=[],
to
到
hiddenimports=["Tkinter", "FileDialog"],
回答by ullix
If the matter is that you don't need Tkinter and friends because you are using PyQt4, then it might be best to avoid loading Tkinter etc altogether. Look into /etc/matplotlibrc and change the defaults to PyQt4, see the 'modified' lines below:
如果问题是因为您使用的是 PyQt4 而不需要 Tkinter 和朋友,那么最好避免完全加载 Tkinter 等。查看 /etc/matplotlibrc 并将默认值更改为 PyQt4,请参阅下面的“修改”行:
#### CONFIGURATION BEGINS HERE
# The default backend; one of GTK GTKAgg GTKCairo GTK3Agg GTK3Cairo
# CocoaAgg MacOSX Qt4Agg Qt5Agg TkAgg WX WXAgg Agg Cairo GDK PS PDF SVG
# Template.
# You can also deploy your own backend outside of matplotlib by
# referring to the module name (which must be in the PYTHONPATH) as
# 'module://my_backend'.
#modified
#backend : TkAgg
backend : Qt4Agg
# If you are using the Qt4Agg backend, you can choose here
# to use the PyQt4 bindings or the newer PySide bindings to
# the underlying Qt4 toolkit.
#modified
#backend.qt4 : PyQt4 # PyQt4 | PySide
backend.qt4 : PyQt4 # PyQt4 | PySide
回答by 101
If you are getting ModuleNotFoundError: No module named ...errors and you:
如果您遇到ModuleNotFoundError: No module named ...错误并且您:
- call PyInstaller from a directory other than your main script
- use relative imports in your script
- 从主脚本以外的目录调用 PyInstaller
- 在脚本中使用相对导入
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
- removing any
__init__.pyfiles (empty__init__.ptfiles are not required in Python 3.3+) or using PyInstaller's
pathsflag 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.
回答by HBK8396
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 .exefile created thereafter executed smoothly, without any module import error.
- 我首先删除了我工作的虚拟环境。
- 使用pip重新安装了所有模块(注意:这次我没有创建任何虚拟环境)。
- 然后我打电话给pyinstaller。
- 之后创建的.exe文件运行顺利,没有任何模块导入错误。

