Python 2.7 PyInstaller运行正常,但exe文件错误:没有名为的模块,无法执行脚本

Python 2.7 PyInstaller运行正常,但exe文件错误:没有名为的模块,无法执行脚本,python-2.7,pyinstaller,Python 2.7,Pyinstaller,我正在运行以下代码: pyinstaller --onefile main.py main.py看起来像: import sys import os sys.path.append(r'C:\Model\Utilities') from import_pythonpkg import * ...... from astroML.density_estimation import EmpiricalDistribution import calendar import collections

我正在运行以下代码:

pyinstaller --onefile main.py
main.py
看起来像:

import sys
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *

......
from astroML.density_estimation import EmpiricalDistribution
import calendar
import collections
from collections import Counter, OrderedDict, defaultdict
import csv

....
import\u pythonpkg.py
看起来像:

import sys
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *

......
from astroML.density_estimation import EmpiricalDistribution
import calendar
import collections
from collections import Counter, OrderedDict, defaultdict
import csv

....
通过在
main.py
上运行
pyinstaller
,成功创建了
main.exe
文件

但是当我运行
main.exe
时,它会给出
astroML
的错误。如果我将
astroML
import\u pythonpkg.py
移动到
main.py
,则
astroML
没有错误。现在,我在csv
csv
中遇到错误

i、 e.如果我将我的
main.py
更改为:

import sys
from astroML.density_estimation import EmpiricalDistribution
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *

......
运行
main.exe
时,
astroML
错误不再存在

import_pythonpkg.py
中的
import calendar
行完全没有错误

pyinstaller
run之后运行
main.exe
时,我不确定如何处理程序包中的此随机错误

import\u pythonpkg
位于
r'C:\Model\Utilities'

编辑:

尽管原始的
main.py
运行正常,但
main.exe的错误如下所示。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
回溯(最近一次呼叫最后一次):
文件“main.py”,第8行,在
文件“C:\Model\Utilities\import\u pythonpkg.py”,第1行,在
从astroML.密度估计导入经验分布
ImportError:没有名为astroML.density_estimation的模块
[29180]无法执行主脚本

我相信PyInstaller没有看到导入pythonpkg
。根据我的经验,当添加到路径或处理外部模块和DLL时,PyInstaller不会去搜索它,您必须明确地告诉它这样做。它会正确地编译成一个.exe,因为它只是忽略它,然后就不会运行了。运行PyInstaller命令时,请检查是否存在有关缺少包或模块的任何警告

但是如何解决它……如果确实是这个问题(我不确定是否是),您可以尝试3种方法:

1) 将该包移动到您的工作目录中,并避免使用
sys.path.append
。然后使用PyInstaller进行编译,看看这是否有效,那么您就知道问题在于PyInstaller无法找到
import\u pythonpkg
。如果这行得通,你可以停在那里

2) 显式地告诉PyInstaller查看那里。在使用PyInstaller进行编译时,可以使用
隐藏导入
标记让它知道(给它完整的路径名)

有关更多信息,请查看此处:

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(...)
有关等级库文件的详细信息:

(请注意,您也可以在此处添加HIDDENIMPORT)


这个答案可能也很有帮助:

它将介绍您计算机上的模块。如果IDE与环境不同,则必须通过pip在设备上加载相同的模块。检查CMD屏幕上的模块,并完成缺失的模块


有时,您必须在设备上加载所有IDE的模块。在我的例子中,有两个ide(pycharm和anaconda)。我使用了pycharm,但pyinstaller使用了anaconda的模块,所以我卸载了anaconda并重试。现在它可以工作了。

您有确切的错误消息吗?pyinstaller可能运行时没有错误,但可能没有包含正确的内容。有什么警告吗?您还可以发布pyinstaller命令的日志,以便我们全面了解正在发生的事情。