python-3.x - runpy 的 pyinstaller 导入错误

标签 python-3.x pyinstaller

我有文件import1.py:

import os
import runpy
filename = r'C:\pyinstallerTest\test.py'
runpy.run_path(filename)

文件test.py是:

import matplotlib.pyplot as plt
import numpy as np
from astroML import *
from tkinter import *

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('About as simple as it gets, folks')
plt.grid(True)
plt.savefig(r"C:\pyinstallerTest\test.png")
plt.show()
print('hello world')

我尝试使用以下命令从 import1.py 创建 exe 文件

pyinstaller --onefile import1.py

import1.exe 文件已成功创建。但是,当我运行 import1.exe 文件时,出现以下错误:

Traceback (most recent call last):
  File "import1.py", line 4, in <module>
  File "runpy.py", line 263, in run_path
  File "runpy.py", line 96, in _run_module_code
  File "runpy.py", line 85, in _run_code
  File "C:\pyinstallerTest\test.py", line 1, in <module>
    import matplotlib.pyplot as plt
ImportError: No module named 'matplotlib'
[1828] Failed to execute script import1

这对我来说更多的是学习练习,所以我并不是在这里寻找替代的更好的做事方式。

最佳答案

PyInstaller 没有看到 matplotlib,因此在编译时它会忽略它,因此在 exe 运行时也看不到它。尝试将其添加为隐藏导入:

pyinstaller --onefile --hidden-import=modulename import1.py

模块名称应该是完整路径,无论它位于何处。

关于python-3.x - runpy 的 pyinstaller 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46931743/

相关文章:

python - Tkinter TkinterHtml 无法运行

python - 编写简洁、灵活且易于维护的用户输入提示

python - 为什么我的pyinstaller python程序在双击而不是在cmd运行时崩溃

python - Pyinstaller ImportError 与 scipy : DLL load failed

从 Python 2.7.X 切换到 3.X 后 MySQL 连接器失败

python - 在 Pandas 中设置联盟

Python socketio badnamespaceerror 在一台设备上出现异常,但在另一台设备上不起作用

python - 异常 : Python. 在 pyinstaller 中找不到运行时

python - OSX 下的 pygst + pyinstaller

python-2.7 - PyInstaller 2.0 类型错误 : compile() expected string without bull bytes