Python 在 PyInstaller 中找不到导入的模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15114695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Imported module not found in PyInstaller
提问by Searene
I'm working in Windows, using PyInstallerto package a python file. But some error is occuring:
我在 Windows 中工作,PyInstaller用于打包 python 文件。但是出现了一些错误:
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 386, in importHook
mod = _self_doimport(nm, ctx, fqname)
File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 480, in doimport
exec co in mod.__dict__
File "D:\Useful Apps\pyinstaller-2.0\server\build\pyi.win32\server\out00-PYZ.pyz\SocketServer", line 132, in <module>
File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 386, in importHook
mod = _self_doimport(nm, ctx, fqname)
File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 480, in doimport
exec co in mod.__dict__
File "D:\Useful Apps\pyinstaller-2.0\server\build\pyi.win32\server\out00-PYZ.pyz\socket", line 47, in <module>
File "D:\Useful Apps\pyinstaller-2.0\PyInstaller\loader\iu.py", line 409, in importHook
raise ImportError("No module named %s" % fqname)
ImportError: No module named _socket
I know that _socketis in path C:\Python27\libs\_socket.lib, but how can let the generated EXEfind that file?
我知道它_socket在 path 中C:\Python27\libs\_socket.lib,但是如何让生成的EXE文件找到该文件?
回答by marius_cornescu
You can add the path to your application spec file.
您可以将路径添加到应用程序规范文件中。
In the Analysisobject you can specify pathex=['C:\Python27\libs\', 'C:\Python27\Lib\site-packages'], and any other path ...
在Analysis对象中,您可以指定pathex=['C:\Python27\libs\', 'C:\Python27\Lib\site-packages'],以及任何其他路径...
Note that if the path is not found there is no problem ... I have paths from linux as well in there.
请注意,如果未找到路径,则没有问题......我在那里也有来自 linux 的路径。
回答by danodonovan
This sounds like a job for hidden imports(only available in the latest builds).
这听起来像是隐藏导入的工作(仅在最新版本中可用)。
From the docs
从文档
a = Analysis(['myscript.py'],
hiddenimports = ['_socket'],
<and everything else>)
回答by karantan
If you are using virtualenv you should use the "-p" or "--path='D:...'" option. Like this:
如果您使用 virtualenv,则应使用“-p”或“--path='D:...'”选项。像这样:
pyinstaller.exe --onefile --paths=D:\env\Lib\site-packages .\foo.py
What this does is generates foo.spec file with this pathex path
这样做是用这个 pathex 路径生成 foo.spec 文件
回答by Korben Dallas
None of the above answers worked for me, but I did get it to work. I was using openpyxl and it required jdcal in the datetime.py module. None of the hidden imports or any of those methods helped, running the exe would still say jdcal not found. The work-around that I used was to just copy the few functions from jdcal directly into the datetime.py in the openpyxl code. Then ran
pyinstaller -F program.py
以上答案都不适合我,但我确实让它起作用了。我使用的是 openpyxl,它需要在 datetime.py 模块中使用 jdcal。隐藏的导入或任何这些方法都没有帮助,运行 exe 仍然会说找不到 jdcal。我使用的解决方法是将 jdcal 中的几个函数直接复制到 openpyxl 代码中的 datetime.py 中。然后跑了
pyinstaller -F program.py
and it worked!
它奏效了!
回答by Korben Dallas
Had similar issues. Here's my fix for PyQt5, cffi, python 3.4.3:
有过类似的问题。这是我对 PyQt5、cffi、python 3.4.3 的修复:
This fixes the 'sip' not found error and the '_cffi_backend' one if that comes up:
这将修复“sip”未找到错误和“_cffi_backend”错误(如果出现):
# -*- mode: python -*-
block_cipher = None
a = Analysis(['LightShowApp.py'],
pathex=['c:\MyProjects\light-show-editor-36',
'c:\Python34\libs\', 'c:\Python34\Lib\site-packages'],
binaries=None,
datas=None,
hiddenimports=['sip', 'cffi'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='LightShowApp',
debug=False,
strip=False,
upx=True,
console=True )
Look at 'pathex' and 'hiddenimports' above. Those are the only changes from default generated. Build exe with:
查看上面的“pathex”和“hiddenimports”。这些是默认生成的唯一更改。使用以下命令构建 exe:
pyinstaller LightShowApp.spec -F
pyinstaller LightShowApp.spec -F
I ran that outside of venv or pip-win - whateverTF that crap is for!
我在 venv 或 pip-win 之外运行了它——不管那些废话是为了什么!
回答by xinthose
In my case, I had to delete all folders and files related to pyinstallerin my directory, i.e. __pycache__, build, dist, and *.spec. I re-ran the build and the exe worked.
就我而言,我不得不删除相关的所有文件夹和文件pyinstaller在我的目录,也就是说__pycache__,build,dist,和*.spec。我重新运行构建并且 exe 工作。

