使用 PyInstaller 时没有命名模块 [英] No module named when using PyInstaller
问题描述
我尝试使用 PyInstaller 在 Windows 7 下编译 Python 项目.该项目运行良好,没有问题,但是当我尝试编译它时,结果不起作用.虽然我在编译过程中没有收到警告,但 build
目录中的 warnmain.txt
文件中有很多警告:warnmain.txt
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 the build
directory: warnmain.txt
我不太理解那些警告,例如没有名为 numpy.pi 的模块",因为 numpy.pi
不是模块而是一个数字.我从未尝试导入 numpy.pi
.我确实明确地导入了 numpy
和 matplotlib
.另外我正在使用 PyQt4.我认为错误可能与那些库有关.
I 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 import numpy.pi
. I did import numpy
and matplotlib
explicitly. In addition I'm using PyQt4. I thought the error might be related to those libraries.
但是我能够编译一个简单的脚本,它成功地使用了 numpy:
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_())
这里的成功意味着创建的可执行文件实际上显示了所需的输出.然而,还创建了一个 warnmain.txt
文件,其中包含与之前完全相同的警告".所以我想编译我的实际项目没有取得任何成功的事实与(或至少不仅)与这些警告无关.但是还有什么可能是错误呢?编译期间唯一的输出是'INFO's,没有一个是否定语句.
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.
我没有指定额外的钩子目录,而是使用默认目录下的钩子,就我可以从编译输出中读取而言,例如hook-matplotlib
已执行.我看不到 numpy
的任何钩子,我的小示例脚本也看不到,但这个有效.我在我的文件中使用了以下导入(并非全部相同但在不同的文件中):
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 for numpy
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
由于 PyInstaller 没有给出任何错误/警告,我无法确定问题是否与库有关,或者是否还有其他需要考虑的问题.
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.
推荐答案
问题是 matplotlib 的一些运行时依赖.所以在运行程序时编译很好,抛出了一些错误.因为终端立即关闭了我没有意识到这一点.将 stdout
和 stderr
重定向到文件后,我可以看到我错过了库 Tkinter
和 FileDialog
.在main顶部添加两个import
就解决了这个问题.
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.
这篇关于使用 PyInstaller 时没有命名模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!