PyInstaller运行良好,但exe文件错误:没有模块命名,无法执行脚本
我运行下面的代码:PyInstaller运行良好,但exe文件错误:没有模块命名,无法执行脚本
pyinstaller --onefile main.py
main.py
样子:
import sys
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *
......
import_pythonpkg.py
样子:
from astroML.density_estimation import EmpiricalDistribution
import calendar
import collections
from collections import Counter, OrderedDict, defaultdict
import csv
....
通过在运行,main.exe
文件已成功创建。
但是,当我运行main.exe
它给出了与astroML
错误。如果我从import_pythonpkg.py
将astroML
移动到main.py
,则astroML
没有错误。现在我遇到了csv
错误。
即如果我改变main.py
为看:
import sys
from astroML.density_estimation import EmpiricalDistribution
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *
......
的astroML
错误不再出现,当我运行main.exe
。
根本没有import calendar
行在import_pythonpkg.py
行错误。
我不确定如何运行pyinstaller
后运行main.exe
运行时如何处理此包随机错误。
import_pythonpkg
位于r'C:\Model\Utilities'
编辑:
错误与main.exe
看起来如下即使原始main.py
运行正常。 Pyinstaller甚至能够让我创建无误的main.exe
。
Traceback (most recent call last):
File "main.py", line 8, in <module>
File "C:\Model\Utilities\import_pythonpkg.py", line 1, in <module>
from astroML.density_estimation import EmpiricalDistribution
ImportError: No module named astroML.density_estimation
[29180] Failed to execute script main
我相信PyInstaller没有看到import_pythonpkg
。根据我的经验,当添加到路径或处理外部模块和dll时,PyInstaller不会去搜索那个,你必须明确地告诉它这样做。它会正确编译为.exe,因为它只是忽略它,但不会运行。在运行PyInstaller命令时,检查是否有关于丢失的软件包或模块的警告。
但如何解决它......如果这确实是问题(这我不知道,这是)你可以尝试3两件事:
1)移动这个包到工作目录,并避免使用sys.path.append
。然后使用PyInstaller进行编译,以查看这是否有效,然后您知道问题是pyinstaller无法找到import_pythonpkg
。如果有效,你可以在那里停下来。
2)明确告诉PyInstaller看看那里。在使用PyInstaller进行编译时,您可以使用hidden-import
标签让它知道(给它全路径名)。
更多信息,请浏览:How to properly create a pyinstaller hook, or maybe hidden import?
3)如果你使用的是PyInstaller创建规范文件,你可以尝试添加一个变量调用pathex
告诉PyInstaller到的东西有搜索:
block_cipher = None
a = Analysis(['minimal.py'],
pathex=['C:\\Program Files (x86)\\Windows Kits\\10\\example_directory'],
binaries=None,
datas=None,
hiddenimports=['path_to_import', 'path_to_second_import'],
hookspath=None,
runtime_hooks=None,
excludes=None,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,...)
coll = COLLECT(...)
关于规范文件的详细信息:https://pyinstaller.readthedocs.io/en/stable/spec-files.html
(注意,你还可以添加hiddenimports这里)
这个答案也许有帮助:PyInstaller - no module named
你是否有确切的错误消息? – The4thIceman
pyinstaller可能运行没有错误,但它可能不包括适当的东西。有没有警告?您还可以发布pyinstaller命令的日志,以便我们掌握正在发生的事情的全貌。 – The4thIceman