使用PyInstaller时未命名模块 [英] No module named when using PyInstaller

查看:85
本文介绍了使用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.我确实明确导入了numpymatplotlib.另外,我正在使用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",并且都不是否定语句.

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的某些运行时依赖项.因此,运行该程序时编译正常,并引发了一些错误.因为终端立即关闭了,所以我没有意识到.将stdoutstderr重定向到文件后,我可以看到我错过了库TkinterFileDialog.在主体顶部添加两个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 imports at the top of the main solved this problem.

这篇关于使用PyInstaller时未命名模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆