简体   繁体   English

在 PyInstaller 中找不到导入的模块

[英]Imported module not found in PyInstaller

I'm working in Windows, using PyInstaller to package a python file.我在 Windows 工作,使用PyInstaller到 package 一个 python 文件。 But some error is occuring:但是发生了一些错误:

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 _socket is in path C:\Python27\libs\_socket.lib , but how can let the generated EXE find that file?我知道_socket在路径C:\Python27\libs\_socket.lib中,但是如何让生成的EXE找到该文件?

If you are using virtualenv you should use the "-p" or "--path='D:...'" option.如果您使用的是 virtualenv,则应使用“-p”或“--path='D:...'”选项。 Like this:像这样:

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

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

You can add the path to your application spec file.您可以将路径添加到应用程序规范文件中。

In the Analysis object you can specify pathex=['C:\\Python27\\libs\\', 'C:\\Python27\\Lib\\site-packages'] , and any other path ...分析对象中,您可以指定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 的路径。

In my case, I had to delete all folders and files related to pyinstaller in my directory, ie __pycache__ , build , dist , and *.spec .就我而言,我必须删除目录中与pyinstaller相关的所有文件夹和文件,即__pycache__builddist*.spec I re-ran the build and the exe worked.我重新运行构建并且 exe 工作。

just delete the '__pycache__' directory then run your exe file again.只需删除'__pycache__'目录,然后再次运行您的 exe 文件。 It worked out for me它对我有用

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.我使用的是 openpyxl,它需要在 datetime.py 模块中使用 jdcal。 None of the hidden imports or any of those methods helped, running the exe would still say jdcal not found.隐藏的导入或任何这些方法都没有帮助,运行 exe 仍然会说找不到 jdcal。 The work-around that I used was to just copy the few functions from jdcal directly into the datetime.py in the openpyxl code.我使用的解决方法是将 jdcal 中的几个函数直接复制到 openpyxl 代码中的 datetime.py 中。 Then ran pyinstaller -F program.py然后运行pyinstaller -F program.py

and it worked!它奏效了!

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.查看上面的“pathex”和“hiddenimports”。 Those are the only changes from default generated.这些是默认生成的唯一更改。 Build exe with:使用以下命令构建 exe:

pyinstaller LightShowApp.spec -F

I ran that outside of venv or pip-win - whateverTF that crap is for!我在 venv 或 pip-win 之外运行了它——不管那些废话是为了什么!

The executor does not know the location of the library, "C:\\Python27\\Lib\\site-packages" etc. Thus, pyinstaller binds the module locations when creating the executable.执行程序不知道库的位置,“C:\\Python27\\Lib\\site-packages”等。因此,pyinstaller 在创建可执行文件时绑定模块位置。 Therefore, you need to import all the modules, you have used into your program.因此,您需要将使用过的所有模块导入到您的程序中。

Import the "_socket" module in your main file and recompile using pyinstaller.在主文件中导入“_socket”模块并使用 pyinstaller 重新编译。

I would probably work.我可能会工作。

Note: But the versions of the modules installed in your system and used in the program must be compatible.注意:但您系统中安装的和程序中使用的模块的版本必须兼容。

If you are using an virtual environment, then problem is because of environment.如果您使用的是虚拟环境,则问题出在环境上。

SOLUTION Just activate the environment and run the pyinstaller command.解决方案只需激活环境并运行 pyinstaller 命令。 For example, If you are using environment of pipenv then run commands in following order.例如,如果您使用的是 pipenv 环境,则按以下顺序运行命令。

pipenv shell # To activate environment

pyintaller --onefile youscript.py # Command to generate executable  

In my case I was trying to import a folder that I created, and ended up here.就我而言,我试图导入我创建的文件夹,并最终出现在此处。 I solved that problem by removing __init__.py from the main folder, keeping the __init__.py in the subfolders that I was importing.我通过从主文件夹中删除__init__.py解决了这个问题,将__init__.py保留在我正在导入的子文件夹中。

Another "In my case" post.另一个“在我的情况下”的帖子。

pypdfium2 (an import in my file that I want to convert to an.exe) has a.dll that it calls called pdfium . pypdfium2 (我要转换为 an.exe 的文件中的导入)具有称为 pdfium 的pdfium pyinstaller doesn't import that.dll when you go to build the.exe by default.默认情况下,当您使用 go 构建.exe 时, pyinstaller不会导入 that.dll。

Fix:使固定:

I think you can do the option --collect-all pypdfium2 ,but at least for me --add-data "C:\Program Files\Python39\Lib\site-packages\pypdfium2\pdfium.dll";.我认为你可以选择--collect-all pypdfium2 ,但至少对我来说--add-data "C:\Program Files\Python39\Lib\site-packages\pypdfium2\pdfium.dll";. (The "." at the end is intentional and needed.) got the job done. (最后的“。”是有意的和需要的。)完成了工作。

I found out that if you make the setup.py for your code and run python setup.py install and then python setup.py build the pyinstaller will be able to find your packages.我发现如果你为你的代码制作 setup.py 并运行python setup.py install然后运行 python python setup.py build pyinstaller 将能够找到你的包。 you can use the find_packages function from setuptools in your setup.py file so it automatically includes everything.您可以在 setup.py 文件中使用 setuptools 中的 find_packages function,这样它会自动包含所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM