gpt4 book ai didi

python-2.7 - PyInstaller 运行良好但 exe 文件错误 : No module named, Failed to Execute Script

转载 作者:行者123 更新时间:2023-12-04 16:08:48 25 4
gpt4 key购买 nike

我正在运行以下代码:

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

....

通过运行 pyinstallermain.py , main.exe文件创建成功。

但是当我运行 main.exe它给出了 astroML 的错误.如果我搬家 astroMLmain.py来自 import_pythonpkg.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根本。

我不确定在运行 main.exe 时如何处理包的这个随机错误后 pyinstaller跑。
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 去那里看看。您可以使用 hidden-import使用 PyInstaller 编译时标记以让它知道(给它完整的路径名)。

--hidden-import=modulename

欲了解更多信息,请点击此处: How to properly create a pyinstaller hook, or maybe hidden import?

3)如果使用PyInstaller创建的spec文件,可以尝试添加变量调用 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

关于python-2.7 - PyInstaller 运行良好但 exe 文件错误 : No module named, Failed to Execute Script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com