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

时间:2017-10-20 23:02:19

标签: 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 *

......

import_pythonpkg.py看起来像:

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

....

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

但是当我运行main.exe时,它会给astroML带来错误。如果我从astroML移动main.pyimport_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

2 个答案:

答案 0 :(得分:1)

我相信PyInstaller没有看到import_pythonpkg。根据我的经验,当添加到路径或处理外部模块和dll时,PyInstaller不会去搜索它,你必须明确告诉它这样做。它会正确编译为.exe,因为它只是忽略它,但不会运行。运行PyInstaller命令时,请检查是否有关于缺少软件包或模块的警告。

但是如何解决它...如果确实这是问题(我不确定它是什么)你可以试试3件事:

1)将该包移动到您的工作目录中,避免使用sys.path.append。然后使用PyInstaller进行编译以查看是否有效,那么您就知道问题是pyinstaller无法找到import_pythonpkg。如果有效,你可以在那里停下来。

2)明确告诉PyInstaller看那里。在使用PyInstaller进行编译时,可以使用hidden-import标记让它知道(给它完整的路径名)。

--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

答案 1 :(得分:0)

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

有时,您必须将设备上的所有IDE装入模块。就我而言,有两个IDE(pycharm和anaconda)。我使用了pycharm,但pyinstaller使用了anaconda的模块,因此我没有安装anaconda并再次尝试。现在就可以了。

相关问题