简体   繁体   English

如何使用 pyinstaller 在可执行文件中包含 .dll 文件?

[英]How do I include .dll file in executable using pyinstaller?

I want to generate a single executable file from my python script.我想从我的 python 脚本生成一个可执行文件。 For this I use pyinstaller.为此,我使用 pyinstaller。 I had issues with mkl libraries because I use numpy in the script.我遇到了 mkl 库的问题,因为我在脚本中使用了 numpy。

I used this hook so solve the issue, it worked fine.我使用了这个钩子来解决这个问题,它工作得很好。 But it does not work if I copy the single executable file to another directory and execute it.但是如果我将单个可执行文件复制到另一个目录并执行它,它就不起作用。 I guess I have to copy the hook also.我想我也必须复制钩子。 But I just want to have one single file that I can use at other computers without copying .dll's or the hook.但我只想拥有一个文件,我可以在其他计算机上使用它而无需复制.dll's或钩子。

I also changed the .spec file as described here and added the necessary files to the binaries -variable.我还按照此处所述更改了.spec文件,并将必要的文件添加到binaries变量中。 That also works as long as the .dll's are in the provided directory for the binaries -variable , but that won't work when I use the executable on a computer that doesn't have these .dll's .只要.dll's位于提供的binaries -variable 目录中,这也有效,但是当我在没有这些.dll's计算机上使用可执行文件时,这将不起作用。

I tried using the --hidden-import= FILENAME option.我尝试使用--hidden-import= FILENAME选项。 This also solves the issue, but just when the .dll's are provided somewhere.这也解决了这个问题,但只是在某处提供了.dll's时。

What I'm looking for is a possibility to bundle the .dll's into the single executable file so that I have one file that works independently.我正在寻找的是一种将.dll's捆绑到单个可执行文件中的可能性,以便我拥有一个独立工作的文件。

When I faced problem described here https://github.com/ContinuumIO/anaconda-issues/issues/443 my workaround was当我遇到这里描述的问题时https://github.com/ContinuumIO/anaconda-issues/issues/443我的解决方法是

pyinstaller -F --add-data vcruntime140.dll;. myscript.py

-F - collect into one *.exe file -F - 收集到一个*.exe文件中

. - Destination path of dll in exe file - exe文件中dll的目标路径

from docs http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files来自文档http://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files

Add the current project folder to Path, then Create EXE using following command:将当前项目文件夹添加到 Path,然后使用以下命令创建 EXE:

pyinstaller --add-binary AutoItX3_x64.dll;. program_name.py

Create folder \\dist\\program_name\\autoit\\lib in tge current project folder, and paste AutoItX3_x64.dll in it.在 tge 当前项目文件夹中创建文件夹\\dist\\program_name\\autoit\\lib ,并将AutoItX3_x64.dll粘贴到其中。

As the selected answer didn't work for the case of using libportaudio64bit.dll , I put my working solution here.由于所选答案不适用于使用libportaudio64bit.dll的情况,我将我的工作解决方案放在这里。

For me, the working solution is to add _sounddevice_data folder where the .exe file is located then making a portaudio-binaries folder in it and finally putting libportaudio64bit.dll in the recently created folder.对我来说,有效的解决方案是在 .exe 文件所在的位置添加_sounddevice_data文件夹,然后在其中创建一个portaudio-binaries文件夹,最后将libportaudio64bit.dll放在最近创建的文件夹中。

Hope it helps!希望有帮助!

Here is a modified version of Ilya's answer.这是 Ilya 答案的修改版本。

pyinstaller --onefile --add-binary ".venv/Lib/site-packages/example_package/example.dll;." myscript.py

It wasn't clear to me when first stumbling into this issue that you must tell PyInstaller exactly where to find the given file (either via relative or absolute path) if it is not already on your PATH .当我第一次遇到这个问题时,我不清楚如果它不在你的PATH上,你必须告诉 PyInstaller 在哪里找到给定的文件(通过相对或绝对路径)。

I have more discussion on how I found exactly which DLL was missing in this answer to a similar question.我有更多关于我如何在类似问题的答案中准确找到缺少哪个 DLL 的讨论。

I much prefer this solution to manually copying DLLs into the export directory, since single EXE is better for distributing utilities to non-programmers.我更喜欢这个解决方案,而不是手动将 DLL 复制到导出目录中,因为单个 EXE 更适合将实用程序分发给非程序员。

暂无
暂无

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

相关问题 如何在 pyinstaller 中包含此文件? - How do I include this file with pyinstaller? 如何通过pyinstaller在可执行文件中包含json - How to include json in executable file by pyinstaller 如何使用PyInstaller从PyCharm项目中创建可执行文件? - How do I create an executable file out of a PyCharm project using PyInstaller? 当我使用 Pyinstaller 将 .py 文件转换为 .exe 时,如何包含输出文件? - How do I include an output file when I convert a .py file to .exe using Pyinstaller? 当我尝试使用 pyinstaller 构建可执行文件时,只创建了 a.spec 文件,如何使用 pyinstaller 构建可执行文件 - When i try to build an executable file with pyinstaller only a .spec file is created, How do i build an executable with pyinstaller 如何在可执行文件中使用 pyinstaller 包含特定的 .jar 文件? - How to include specific .jar files with pyinstaller in executable file? 如何通过我的规范文件说服 PyInstaller 在它制作的 EXE 中包含 libvlc.dll? - How can I convince PyInstaller, via my spec file, to include libvlc.dll in the EXE it makes? 如何使用 pyinstaller 包含文件? - How do I include files with pyinstaller? 如何将图像添加到 onefile pyinstaller 可执行文件中? - How do I add an image into a onefile pyinstaller executable? 如何使用 pyinstaller 将多个 .py 文件构建为单个可执行文件? - How to build multiple .py files into a single executable file using pyinstaller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM