简体   繁体   English

如何在Python中创建可执行文件?

[英]How to make an executable file in Python?

I want to make an executable file (.exe) of my Python application. 我想制作我的Python应用程序的可执行文件(.exe)。

I want to know how to do it but have this in mind: I use a C++ DLL! 我想知道如何做到这一点,但请记住:我使用C ++ DLL!

Do I have to put the DLL along side with the .exe or is there some other way? 我是否必须将DLL与.exe放在一边或者还有其他方法吗?

py2exe can generate single file executables. py2exe可以生成单个文件可执行文件。 see this link for examples. 请参阅此链接以获取示例。

The setup.py I use uses the following combination of options: 我使用的setup.py使用以下选项组合:

'compressed': 1,
 'optimize':2,
 'bundle_files': 1

I usually add external dlls (pe msvcr71.dll) in the same folder where the executable is. 我通常在可执行文件所在的同一文件夹中添加外部dll(pe msvcr71.dll)。

To facilitate distribution and to automate installation after generating your exe you can use Inno Setup to create an installer that puts your exe and your dlls, and other documents (readme, etc) into the application directory 为了便于分发并在生成exe后自动安装,您可以使用Inno Setup创建一个安装程序,将您的exe和dll以及其他文档(自述文件等)放入应用程序目录中

Follow These Steps: You can find this documentation in this site . 请遵循以下步骤:您可以在此站点中找到此文档。

1) Install PyInstaller: 1)安装PyInstaller:

Assuming you have PIP installed in this directory c:\\PythonXX\\Scripts if not go to this site and see instructions on how to "Install Python Indexing Project (PIP)". 假设您在此目录中安装了PIP c:\\ PythonXX \\ Scripts,如果没有,请访问此站点并查看有关如何“安装Python索引项目(PIP)”的说明。 Go to your command prompt and type the following command. 转到命令提示符并键入以下命令。

  • cd c:\\Python27\\Scripts to go to this directory press Enter cd c:\\ Python27 \\ Scripts转到此目录按Enter键
  • Then type -> pip install pyinstaller press Enter again 然后键入 - > pip install pyinstaller再次按Enter键
  • This should install PyInstaller successfully! 这应该成功安装PyInstaller!
  • Obs: You don't need to have pyinstaller inside the Scripts folder after installation this was done because we needed PIP to install the pyinstaller files. Obs:安装完成后,您不需要在Scripts文件夹中安装pyinstaller,因为我们需要PIP来安装pyinstaller文件。 To make things more organized I usually move the pyinstaller files to another folder. 为了使事情更有条理,我通常将pyinstaller文件移动到另一个文件夹。 To move the pysintaller files go to the Scripts folder where the pyinstaller files are located and look for all the files that start with "pyi" you can also look at the Modified Date column to see which files where created at the same time. 要移动pysintaller文件,请转到pyinstaller文件所在的Scripts文件夹,并查找以“pyi”开头的所有文件,您还可以查看“修改日期”列以查看同时创建的文件。 It should be 24 pyi files that where created when you ran the script above. 它应该是24个pyi文件,在您运行上面的脚本时创建。 Copy those files into another folder of your choice I usually create a folder within the C:\\ drive and call it "CreateExecutables" with another folder within called "pyinstaller_files". 将这些文件复制到您选择的另一个文件夹中我通常在C:\\驱动器中创建一个文件夹,并将其称为“CreateExecutables”,其中包含名为“pyinstaller_files”的另一个文件夹。

2) Install PyWin32: 2)安装PyWin32:

Go to this SITE and look for the executable named pywin32-218.win32-py2.7.exe 2.7 which is for 32 bit systems for python27 look for the one that corresponds to you. 转到此站点并查找名为pywin32-218.win32-py2.7.exe 2.7的可执行文件,该文件用于32位系统,用于python27查找与您对应的系统。 Run the executable file and it should install PyWin32 successfully which works hand in hand with pyinstaller! 运行可执行文件,它应该成功安装PyWin32,它与pyinstaller携手合作!

3) Create Single Executable file 3)创建单个可执行文件

Now that you have PyInstaller and PyWin32 you can start to create the single executable file. 现在您已经拥有了PyInstaller和PyWin32,您可以开始创建单个可执行文件。 The single executable file will be created inside another folder called “dist” this folder is also created dynamically when you run the pyinstaller command. 单个可执行文件将在另一个名为“dist”的文件夹中创建,此文件夹也是在运行pyinstaller命令时动态创建的。 To make things clear your pyinstaller files where copied by you to another folder probably called "pyinstaller_files" folder so now type the following command in your command prompt. 为了清楚你的pyinstaller文件被你复制到另一个可能被称为“pyinstaller_files”文件夹的文件夹,所以现在在命令提示符下键入以下命令。

  • c:\\python27\\CreateExecutables\\pyinstaller_files>pyinstaller --onefile c:\\test\\test1\\yourscript.py c:\\ python27 \\ CreateExecutables \\ pyinstaller_files> pyinstaller --onefile c:\\ test \\ test1 \\ yourscript.py
  • Obs: The directory of yourscript.py is where you have your pydev module located. Obs:yourscript.py的目录是你的pydev模块所在的位置。

4) Overview 4)概述

The above command will create a folder called “dist” inside the pyinstaller_files folder this folder will contain your single executable file “yourscript.exe”. 上面的命令将在pyinstaller_files文件夹中创建一个名为“dist”的文件夹,该文件夹将包含您的单个可执行文件“yourscript.exe”。 You can now move the single executable file somewhere of your choice and delete the “dist” & “build” folder with the “yourscript.spec” file as they are no longer needed to run your single executable file. 您现在可以将单个可执行文件移动到您选择的位置,并使用“yourscript.spec”文件删除“dist”和“build”文件夹,因为不再需要它们来运行您的单个可执行文件。

Use py2exe . 使用py2exe You can put the DLL in the same folder as the final EXE and it should be loaded fine. 您可以将DLL放在与最终EXE相同的文件夹中,它应该可以正常加载。

I've built exe files from Python 2.7 code using each of these tools: 我使用以下每个工具从Python 2.7代码构建了exe文件:

  • Cython (with --embed option) Cython(带--embed选项)
  • Nuitka Nuitka
  • Py2exe Py2exe

These all will produce a standalone exe. 这些都将产生一个独立的exe。 Your installer will have to include the requisite CRT dlls. 您的安装人员必须包含必要的CRT dll。 Your end result will be indistinguishable from any other exe to the typical user. 您的最终结果将与典型用户的任何其他exe无法区分。

The first two compile to C first, then compile that to a final exe using whatever compiler you have installed. 前两个首先编译为C,然后使用您安装的任何编译器将其编译为最终的exe。 Py2Exe bundles Python and all your .pyo files into a zip which it embeds in the exe. Py2Exe将Python和所有.pyo文件捆绑到一个zip中,它嵌入到exe中。 When launched it unzips to the temp directory and runs the python from there. 启动时,它会解压缩到temp目录并从那里运行python。 You'll need to add your dll to the .spec file so it is included in the zip resource. 您需要将您的dll添加到.spec文件中,以便它包含在zip资源中。 Alternatively, install your dll alongside the exe and load it using an absolute path in python. 或者,在exe旁边安装你的dll并使用python中的绝对路径加载它。

Have a look at pyinstaller . 看看pyinstaller It should find (and correspondingly ship) these dependencies for you. 它应该为您找到(并相应地发布)这些依赖项。

cx_Freeze is a good one as well. cx_Freeze也是一个很好的。

cx_Freeze is a set of scripts and modules for freezing Python scripts into executables cx_Freeze是一组用于将Python脚本冻结为可执行文件的脚本和模块

PyInstaller is the best choice, because it works well on both Windows and Linux. PyInstaller是最好的选择,因为它在Windows和Linux上运行良好。 If you are using arch linux , use this link: PyInstaller or try yaourt pyinstaller . 如果您使用的是arch linux ,请使用以下链接: PyInstaller或尝试yaourt pyinstaller

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

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