[英]Import error when using pyinstaller to create a single exe file
I have a python package that involves several python modules. 我有一个涉及多个python模块的python软件包。 The project has been created in pycharm.
该项目已在pycharm中创建。 I have used pyinstaller to create a single executable file of my python package.
我已经使用pyinstaller创建了我的python包的单个可执行文件。 When I run the executable using a batch file I get an import error, specifically pandas has failed to be imported.
当我使用批处理文件运行可执行文件时,出现导入错误,特别是熊猫导入失败。 Is there a reason why pyinstaller has not also collected the package dependencies in my virtual environment?
pyinstaller还没有在我的虚拟环境中还收集软件包依赖项的原因吗?
Thanks in advance for any help! 在此先感谢您的帮助!
I have the following 2 errors: (1) ModuleNotFoundError: No module named 'pandas._libs.tslibs.np_datetime' 我有以下2个错误:(1)ModuleNotFoundError:没有名为“ pandas._libs.tslibs.np_datetime”的模块
(2) File "site-packages\\pandas__init__.py", line 35, in ImportError: C extension: No module named 'pandas._libs.tslibs.np_datetime' not built. (2)在ImportError:C扩展名的第35行,文件“ site-packages \\ pandas__init __。py”:没有建立名为'pandas._libs.tslibs.np_datetime'的模块。 If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.
如果要从源目录导入熊猫,可能需要运行“ python setup.py build_ext --inplace --force”以首先构建C扩展。
There is one pip script for each virtual environment. 每个虚拟环境都有一个pip脚本。 So when you install a python module it get installed into the projectname\\venv\\Lib\\site-packages directory.
因此,当您安装python模块时,它将安装到projectname \\ venv \\ Lib \\ site-packages目录中。
When you run pyinstaller from terminal to make the executable, pyinstaller checks for dependencies in Sys.path . 当您从终端运行pyinstaller以生成可执行文件时,pyinstaller会检查Sys.path中的依赖项 。 But that path does not include the projectname\\venv\\Lib\\site-packages directory.
但是该路径不包括projectname \\ venv \\ Lib \\ site-packages目录。 Therefore pyinstaller cannot find those particular dependencies.
因此,pyinstaller无法找到那些特定的依赖项。 In such cases it gives you warnings.Those warning can be found in 'warnname.txt' near your executable file.
在这种情况下,它会向您发出警告。可以在可执行文件附近的“ warnname.txt”中找到这些警告。
EDIT: How to Configure pycharm to run pyinstaller 编辑: 如何配置pycharm以运行pyinstaller
Then you need to setup running configurations. 然后,您需要设置运行配置。
Script name: path to your python script 脚本名称: python脚本的路径
working path: Project location 工作路径:项目所在地
leave interpreter options as it is in the image. 保留解释器选项在图像中的位置。
Run pyinstaller. 运行pyinstaller。 You can find your .exe in dist directory .
您可以在dist目录中找到您的.exe 。
If the "Module not found" error still persists. 如果“找不到模块”错误仍然存在。 You can add a hidden import hook and specify the names of the missing modules.Navigate to
您可以添加一个隐藏的导入挂钩并指定缺少的模块的名称。
Project Path\\venv\\Lib\\site-packages\\PyInstaller\\hooks
项目路径\\ venv \\ Lib \\ site-packages \\ PyInstaller \\ hooks
and create a new "hook-pandas.py" (hook-modulename.py) script and make a list of hidden import modules like this. 并创建一个新的“ hook-pandas.py” (hook-modulename.py)脚本,并列出这样的隐藏导入模块列表。
hiddenimports = ['pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.nattype','pandas._libs.skiplist']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.