[英]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 . 请遵循以下步骤:您可以在此站点中找到此文档。
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.
转到命令提示符并键入以下命令。
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携手合作!
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”文件夹的文件夹,所以现在在命令提示符下键入以下命令。
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”文件夹,因为不再需要它们来运行您的单个可执行文件。
I've built exe files from Python 2.7 code using each of these tools: 我使用以下每个工具从Python 2.7代码构建了exe文件:
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.
它应该为您找到(并相应地发布)这些依赖项。
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.