简体   繁体   English

使用 PyInstaller 时没有命名模块

[英]No module named when using PyInstaller

I try to compile a Python project under Windows 7 using PyInstaller.我尝试使用 PyInstaller 在 Windows 7 下编译一个 Python 项目。 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虽然我在编译过程中没有收到警告,但build目录中的warnmain.txt文件中有很多警告: warnmain.txt

I don't really understand those warnings, for example "no module named numpy.pi" since numpy.pi is no module but a number.我不太理解那些警告,例如“没有名为 numpy.pi 的模块”,因为numpy.pi不是模块而是一个数字。 I never tried to import numpy.pi .我从未尝试导入numpy.pi I did import numpy and matplotlib explicitly.我确实明确地导入numpymatplotlib In addition I'm using PyQt4.另外我正在使用 PyQt4。 I thought the error might be related to those libraries.我认为错误可能与这些库有关。

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.txt file created which contains exactly the same 'warnings' as the one before.这里成功意味着创建的可执行文件实际上显示了所需的 output。但是,还创建了一个warnmain.txt文件,其中包含与之前完全相同的“警告”。 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.编译期间唯一的 output 是 'INFO',没有一个是否定语句。

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, eg hook-matplotlib was executed.我没有指定额外的挂钩目录,但挂钩使用默认目录,据我从编译 output 中读取,例如hook-matplotlib已执行。 I could not see any hook for numpy neither could I for my small example script but this one worked.我看不到numpy的任何挂钩,我的小示例脚本也看不到,但这个有效。 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.由于 PyInstaller 没有给出任何错误/警告,我无法确定问题是否与库有关,或者是否还有其他需要考虑的问题。

Had a similar problem with no module named FileDialog . no module named FileDialog有类似的问题。 Discovered that with version 3.2, I could use发现使用 3.2 版,我可以使用

pyinstaller --hidden-import FileDialog ...

instead of modifying my main script.而不是修改我的主脚本。

Pyinstaller won't see second level imports . Pyinstaller 不会看到二级导入 So if you import module A , pyinstaller sees this.因此,如果您导入模块A , pyinstaller 会看到这一点。 But any additional module that is imported in A will not be seen.但是在A 中导入的任何附加模块都不会被看到。

There is no need to change anything in your python scripts.无需更改 Python 脚本中的任何内容。 You can directly add the missing imports to the spec file .您可以直接将缺少的导入添加到规范文件中 Just change the following line:只需更改以下行:

hiddenimports=[],

to

hiddenimports=["Tkinter", "FileDialog"],

The problem were some runtime dependencies of matplotlib.问题是 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 .stdoutstderr重定向到文件后,我可以看到我错过了库TkinterFileDialog Adding two import s at the top of the main solved this problem.在 main 顶部添加两个import解决了这个问题。

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从主脚本以外的目录调用 PyInstaller
  • 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从与主脚本相同的目录调用 PyInstaller
  • removing any __init__.py files (empty __init__.pt files are not required in Python 3.3+)删除任何__init__.py文件(Python 3.3+ 中不需要空的__init__.pt文件)
  • or using PyInstaller's paths flag to specify a path to search for imports .或使用 PyInstaller 的paths标志来指定搜索导入路径 Eg if you are calling PyInstaller from a parent folder to your main script, and your script lives in subfolder , then call PyInstaller as such:例如,如果您从父文件夹调用 PyInstaller 到您的主脚本,并且您的脚本位于subfolder ,则调用 PyInstaller 如下:

    pyinstaller --paths=subfolder subfolder/script.py . pyinstaller --paths=subfolder subfolder/script.py

I was facing the same problem and the following solution worked for me:我遇到了同样的问题,以下解决方案对我有用:

  1. I first removed the virtual environment in which I was working.我首先删除了我工作的虚拟环境。
  2. Reinstalled all the modules using pip (note: this time I did not create any virtual environment).使用pip重新安装了所有模块(注意:这次我没有创建任何虚拟环境)。
  3. Then I called the pyinstaller .然后我打电话给pyinstaller
  4. The .exe file created thereafter executed smoothly, without any module import error.之后创建的.exe文件运行顺利,没有任何模块导入错误。

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.如果问题是因为您使用的是 PyQt4 而不需要 Tkinter 和朋友,那么最好避免完全加载 Tkinter 等。 Look into /etc/matplotlibrc and change the defaults to PyQt4, see the 'modified' lines below:查看 /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

I had the same problem with pyinstaller 3.0 and weblib .我在 pyinstaller 3.0 和weblib遇到了同样的问题。 Importing it in the main didn't help.在主要导入它没有帮助。

Upgrading to 3.1 and deleting all build files helped.升级到 3.1 并删除所有构建文件有帮助。

pip install --upgrade pyinstaller

可能不是一个好习惯,但在我的项目中使用的原始环境(而不是单独的 venv)中安装 pyinstaller 有助于解决 ModuleNotFoundError

I had similar problem with PySimpleGUI.我在 PySimpleGUI 上遇到了类似的问题。 The problem was, pyinstaller was installed in different directory.问题是,pyinstaller 安装在不同的目录中。 SOLUTION (solved for me) : just install pyinstaller in the same directory in which the file is present (that to be converted to exe)解决方案(为我解决):只需将 pyinstaller 安装在文件所在的同一目录中(要转换为 exe)

If these solutions don't work, simply deleting and reinstalling pyinstaller can fix this for you (as it did for me just now).如果这些解决方案不起作用,只需删除并重新安装 pyinstaller 即可为您解决此问题(就像刚才对我所做的那样)。

Putting this here for anyone else who might come across this post.把这个放在这里给任何可能遇到这篇文章的人。

I had the same error.我有同样的错误。 Mine said "ModuleNotFoundError: No module named 'numpy'".我的说“ModuleNotFoundError:没有名为'numpy'的模块”。 I fixed it by typing the following in the cmd:我通过在 cmd 中键入以下内容来修复它:

pip install pyinstaller numpy pip 安装 pyinstaller numpy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM