Python PyInstaller,规范文件,导入错误:没有名为';废话';

Python PyInstaller,规范文件,导入错误:没有名为';废话';,python,mysql,pyinstaller,importerror,Python,Mysql,Pyinstaller,Importerror,我正在尝试通过构建一个python脚本。我使用了以下命令来配置、生成等级库文件和生成: wget pyinstaller.zip, extracted it, python Configure.py, etc, then: python pyinstaller/Makespec.py --onefile myscript.py python pyinstaller/Build.py myscript.spec 以下是它生成的等级库文件: # -*- mode: python -*- a =

我正在尝试通过构建一个python脚本。我使用了以下命令来配置、生成等级库文件和生成:

wget pyinstaller.zip, extracted it, python Configure.py, etc, then:

python pyinstaller/Makespec.py --onefile myscript.py
python pyinstaller/Build.py myscript.spec 
以下是它生成的等级库文件:

# -*- mode: python -*-
a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'), os.path.join(HOMEPATH,'support/useUnicode.py'), 'icinga.py'],
             pathex=['/home/user/projects/icinga_python/releases/v2.1'])
pyz = PYZ(a.pure)
exe = EXE( pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name=os.path.join('dist', 'myscript'),
          debug=False,
          strip=False,
          upx=True,
          console=1 )
这在
dist/
目录中构建了一个可执行文件。尝试运行此文件时,我得到以下信息:

Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 455, in importHook
    raise ImportError, "No module named %s" % fqname
ImportError: No module named mysql
回溯(最近一次呼叫最后一次):
文件“”,第12行,在
importHook中的文件“/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py”,第455行
“没有名为%s”%fqname的模块”
ImportError:没有名为mysql的模块
如果我将此可执行文件移动到实际Python代码的目录中,则会得到不同的结果:

Traceback (most recent call last):
  File "<string>", line 12, in <module>
  File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 436, in importHook
    mod = _self_doimport(nm, ctx, fqname)
  File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 521, in doimport
    exec co in mod.__dict__
  File "CLUSTER/mysql/icingasql.py", line 13, in <module>
    import urllib2
  File "/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py", line 455, in importHook
    raise ImportError, "No module named %s" % fqname
ImportError: No module named urllib2
回溯(最近一次呼叫最后一次):
文件“”,第12行,在
importHook中的文件“/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py”,第436行
mod=\u self\u doimport(nm、ctx、fqname)
文件“/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py”,第521行,在doimport中
执行公司在国防部__
文件“CLUSTER/mysql/icingasql.py”,第13行,在
导入urllib2
importHook中的文件“/home/user/projects/myscript/releases/v2.1/pyinstaller/iu.py”,第455行
“没有名为%s”%fqname的模块”
ImportError:没有名为urllib2的模块
在。。。我看到
--onefile
是我需要/想要的选项,但由于某些原因,并不是所有的东西都在编译中


脚本并没有包含任何花哨的东西,只是我为sql语句编写的一些快速模块,以及解析某些网站。

当代码中有动态导入时,可能会出现此错误。在这种情况下,pyinstaller不会在exe文件中包含这些包。在这种情况下,您可以:

  • 在代码中添加这些包的未使用导入

  • 一个文件选项在运行代码时不会改变任何内容。如果创建--onefile exe,pyinstaller创建的所有文件都打包到exe文件,并在每次运行exe时解包到本地临时文件。

    问题在于pyinstaller不会看到第二级导入。因此,如果导入模块A,pyinstaller会看到这一点。但是在中导入的任何附加模块都不会被看到

    不需要在python脚本中更改任何内容。您可以直接将缺少的导入添加到规范文件中。 只需在
    a=Analysis(…)
    中添加以下内容:

    结果应该是:

    a = Analysis([os.path.join(HOMEPATH,'support/_mountzlib.py'), os.path.join(HOMEPATH,'support/useUnicode.py'), 'icinga.py'],
             pathex=['/home/user/projects/icinga_python/releases/v2.1'], hiddenimports=["mysql"],)
    

    之后,以规范文件为参数运行pyinstaller。

    只需添加我的2美分,因为我今天遇到了同样的问题-6年后:D

    对于Windows:

    1) cmd => rightclick => with admin rights
    2) Enter in cmd: "pip install pyinstaller"
    3) navigate in cmd to the folder of "yourMain.py"
    4) Enter in cmd: "pyinstaller --onefile --windowed yourMain.py"
    
    5) If you import other scripts / data in "yourMain.py": 
    Manually enter the folder "dist" (gets created - where "yourMain.exe" should be by now), 
    and copy your scripts or folder structure there
    
    (e.g. /assets/sounds; /assets/graphics; /scripts; anotherscript.py )
    
    然后我可以通过双击来运行exe

    结果证明这很容易。我的诀窍是使用“-onfile”并将其他文件添加到“dist”文件夹中


    “--windowed”只是为了在启动exe时不会弹出python命令窗口

    thanx+1!注意:以后运行pyinstaller时,请提供.spec文件作为参数,而不是.py文件!(详细阅读问题,我不是第一次…;))这不是真的。PyInstaller将查找二级导入。然而,有时模块是动态导入的。(导入是在带有importlib的代码中完成的),因此PyInstaller无法找到模块。在这些情况下,隐藏的import子句可以解决您的问题。此外,在大多数情况下,您不需要单独运行分析,如果您将hidden imports子句添加到pyinstaller命令中,它将找到模块。您手动将脚本添加到dist文件夹的解决方法最终对我有效。但是,还是应该有另一个解决方案。。。
    1) cmd => rightclick => with admin rights
    2) Enter in cmd: "pip install pyinstaller"
    3) navigate in cmd to the folder of "yourMain.py"
    4) Enter in cmd: "pyinstaller --onefile --windowed yourMain.py"
    
    5) If you import other scripts / data in "yourMain.py": 
    Manually enter the folder "dist" (gets created - where "yourMain.exe" should be by now), 
    and copy your scripts or folder structure there
    
    (e.g. /assets/sounds; /assets/graphics; /scripts; anotherscript.py )