简体   繁体   English

使用pyinstaller创建单个exe文件时导入错误

[英]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

  1. First you need to add pyinstaller into project interpreter. 首先,您需要将pyinstaller添加到项目解释器中。 在此处输入图片说明
  2. 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. 保留解释器选项在图像中的位置。

  3. Run pyinstaller. 运行pyinstaller。 You can find your .exe in dist directory . 您可以在dist目录中找到您的.exe

  4. 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']
  1. And run pyinstaller again, and it should work now. 并再次运行pyinstaller,它现在应该可以工作了。

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

相关问题 python pyinstaller 创建单个文件导入目录 - python pyinstaller create single file import directory 使用 pyinstaller 将“.py 文件”(具有“导入光栅”)转换为“.exe 文件”时出现“导入错误:DLL 加载失败” - "Import Error: DLL load failed" while converting ".py file" (having "import rasterio") to ".exe file" using pyinstaller Pyinstaller 运行 exe 文件时出错 - Pyinstaller Error when running the exe file 使用pyinstaller时过大的.exe文件 - Overly large .exe file when using pyinstaller 使用 pyinstaller 导入 external.exe 文件 - Import external .exe file with pyinstaller 当需要包含非 python 文件时,使用 PyInstaller 创建单个 exe? - Using PyInstaller to create single exe when there are non python files that need to be included? 错误方言 = eval(dialects.strip()) 文件“<string> ",尝试在 PyInstaller 中创建 exe 文件时的第 0 行</string> - Error dialects = eval(dialects.strip()) File "<string>", line 0 when try to create exe file in PyInstaller 如何使用pyinstaller将多个python文件编译成单个.exe文件 - How to compile multiple python files into single .exe file using pyinstaller "使用 pyinstaller 编译为单个 exe 时找不到 .Kv 文件" - .Kv file not found while compiling to single exe using pyinstaller Python 使用 pyinstaller 创建 exe 文件 - Python create exe file with pyinstaller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM