将简单的Python脚本转换为Windows可执行文件的过程

时间:2021-08-10 20:26:22

I wrote a script that will help a Windows user in her daily life. I want to simply send her the .exe and not ask her to install python, dlls or have to deal with any additional files.

我写了一个脚本,将帮助Windows用户在她的日常生活。我只想给她发送.exe,而不是让她安装python、dll或必须处理任何其他文件。

I've read plenty of the * entries regarding compiling Python scripts into executable files. I am a bit confused as there are many options but some seem dated (no updates since 2008) and none were simple enough for me not to be asking this right now after a few hours spent on this.

我已经阅读了大量关于将Python脚本编译成可执行文件的*条目。我有点困惑,因为有很多选择,但有些似乎过时了(自2008年以来没有更新),而且没有一个简单到让我在花了几个小时的时间之后不会马上问这个问题。

I'm hoping there's a better, up-to-date way to do this.

我希望有一种更好的、最新的方法来做到这一点。

I looked into:

我看着:

but either I couldn't get them to work or couldn't understand how to get the result I need. The closest I got was with py2exe but it still gave me the MSVCR71.dll

但要么我不能让他们工作,要么我不明白如何得到我需要的结果。我最接近的是py2exe,但它仍然给了我MSVCR71.dll。

I would appreciate a step-by-step answer as I was also unable to follow some of the tweaking answers here that require some prior understanding of how to use py2exe or some of the other tools.

我将非常感谢一个循序渐进的回答,因为我也无法理解一些需要事先了解如何使用py2exe或其他工具的问题。

I'm using Python 2.5 as one of the modules is only available for that version.

我使用Python 2.5作为其中一个模块,它只适用于那个版本。

7 个解决方案

#1


26  

PyInstaller will create a single-file executable if you use the --onefile option (though what it actually does is extracts then runs itself).

PyInstaller将创建一个单文件可执行文件,如果您使用-onefile选项(尽管它实际上做的是提取然后运行它自己)。

There's a simple PyInstaller tutorial here. If you have any questions about using it, please post them...

这里有一个简单的PyInstaller教程。如果您对使用它有任何问题,请发布它们……

#2


13  

Using py2exe, include this in your setup.py:

使用py2exe,在setup.py中包含这个。

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    windows = [{'script': "YourScript.py"}],
    zipfile = None,
)

then you can run it through command prompt / Idle, both works for me. Hope it helps

然后你可以通过命令提示符/ Idle运行它,这两个都对我有用。希望它能帮助

#3


11  

i would recommend going to http://sourceforge.net/projects/py2exe/files/latest/download?source=files to download py2exe. Then make a python file named setup.py. Inside it, type

我建议您访问http://sourceforge.net/projects/py2exe/files/latest/download?源=文件下载py2exe。然后创建一个名为setup.py的python文件。里面,输入

from distutils.core import setup
import py2exe
setup(console=['nameoffile.py'])

Save in your user folder Also save the file you want converted in that same folder

保存在您的用户文件夹中也保存您想要转换的文件在同一个文件夹。

Run window's command prompt type in setup.py install py2exe

在设置中运行窗口的命令提示类型。py安装py2exe

It should print many lines of code...

它应该打印许多行代码……

Next, open the dist folder.

接下来,打开dist文件夹。

Run the exe file.

运行exe文件。

If there are needed files for the program to work, move them to the folder

如果程序需要文件,请将它们移动到文件夹中

Copy/Send the dist folder to person.

复制/发送dist文件夹给person。

Optional: Change the name of the dist folder

可选:更改dist文件夹的名称

Hope it works!:)

希望它工作!:)

#4


8  

I would join @Nicholas in recommending PyInstaller (with the --onefile flag), but be warned: do not use the "latest release", PyInstaller 1.3 -- it's years old. Use the "pre-release" 1.4, download it here -- or even better the code from the svn repo -- install SVN and run svn co http://svn.pyinstaller.org/trunk pyinstaller.

我将与@Nicholas一起推荐PyInstaller(带有——onefile标志),但要注意:不要使用“最新版本”PyInstaller 1.3——它已经使用多年了。使用“预发布”1.4,在这里下载它——或者更好地从svn repo下载代码——安装svn并运行svn co http://svn.pyinstaller.org/trunk pyinstaller。

As @Nicholas implies, dynamic libraries cannot be run from the same file as the rest of the executable -- but fortunately they can be packed together with all the rest in a "self-unpacking" executable that will unpack itself into some temporary directory as needed; PyInstaller does a good job at this (and at many other things -- py2exe is more popular, but pyinstaller in my opinion is preferable in all other respects).

正如@Nicholas所暗示的,动态库不能从与可执行文件的其他部分相同的文件中运行——但幸运的是,它们可以与所有其他部分打包在一个“自解包”可执行文件中,该可执行文件将自己解压缩到某个临时目录中;PyInstaller在这方面做得很好(在许多其他方面——py2exe更受欢迎,但在我看来,PyInstaller在其他所有方面都更可取)。

#5


6  

1) Get py2exe from here, according to your Python version.

从这里得到py2exe,根据您的Python版本。

2) Make a file called "setup.py" in the same folder as the script you want to convert, having the following code:

2)制作一个名为“setup”的文件。在您要转换的脚本所在的文件夹中,有以下代码:

from distutils.core import setup
import py2exe
setup(console=['myscript.py']) #change 'myscript' to your script

3) Go to command prompt, navigate to that folder, and type:

3)转到命令提示符,导航到该文件夹,输入:

python setup.py py2exe

4) It will generate a "dist" folder in the same folder as the script. This folder contains the .exe file.

4)它将在与脚本相同的文件夹中生成一个“dist”文件夹。此文件夹包含.exe文件。

#6


2  

you may want to see if your app can run under IronPython. If so, you can compile it to an exe http://www.codeplex.com/IronPython

您可能想看看您的应用程序是否可以在IronPython下运行。如果是这样,您可以将它编译为exe http://www.codeplex.com/IronPython

#7


0  

You could create an installer for you EXE file by:
1. Press WinKey + R
2. Type "iexpress" (without quotes) into the run window
3. Complete the wizard for creating the installation program.
4. Distribute the completed EXE.

您可以通过:1为您的EXE文件创建一个安装程序。按WinKey + r2。在运行窗口3中输入“iexpress”(不带引号)。完成创建安装程序的向导。4所示。EXE分配完成。

#1


26  

PyInstaller will create a single-file executable if you use the --onefile option (though what it actually does is extracts then runs itself).

PyInstaller将创建一个单文件可执行文件,如果您使用-onefile选项(尽管它实际上做的是提取然后运行它自己)。

There's a simple PyInstaller tutorial here. If you have any questions about using it, please post them...

这里有一个简单的PyInstaller教程。如果您对使用它有任何问题,请发布它们……

#2


13  

Using py2exe, include this in your setup.py:

使用py2exe,在setup.py中包含这个。

from distutils.core import setup
import py2exe, sys, os

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    windows = [{'script': "YourScript.py"}],
    zipfile = None,
)

then you can run it through command prompt / Idle, both works for me. Hope it helps

然后你可以通过命令提示符/ Idle运行它,这两个都对我有用。希望它能帮助

#3


11  

i would recommend going to http://sourceforge.net/projects/py2exe/files/latest/download?source=files to download py2exe. Then make a python file named setup.py. Inside it, type

我建议您访问http://sourceforge.net/projects/py2exe/files/latest/download?源=文件下载py2exe。然后创建一个名为setup.py的python文件。里面,输入

from distutils.core import setup
import py2exe
setup(console=['nameoffile.py'])

Save in your user folder Also save the file you want converted in that same folder

保存在您的用户文件夹中也保存您想要转换的文件在同一个文件夹。

Run window's command prompt type in setup.py install py2exe

在设置中运行窗口的命令提示类型。py安装py2exe

It should print many lines of code...

它应该打印许多行代码……

Next, open the dist folder.

接下来,打开dist文件夹。

Run the exe file.

运行exe文件。

If there are needed files for the program to work, move them to the folder

如果程序需要文件,请将它们移动到文件夹中

Copy/Send the dist folder to person.

复制/发送dist文件夹给person。

Optional: Change the name of the dist folder

可选:更改dist文件夹的名称

Hope it works!:)

希望它工作!:)

#4


8  

I would join @Nicholas in recommending PyInstaller (with the --onefile flag), but be warned: do not use the "latest release", PyInstaller 1.3 -- it's years old. Use the "pre-release" 1.4, download it here -- or even better the code from the svn repo -- install SVN and run svn co http://svn.pyinstaller.org/trunk pyinstaller.

我将与@Nicholas一起推荐PyInstaller(带有——onefile标志),但要注意:不要使用“最新版本”PyInstaller 1.3——它已经使用多年了。使用“预发布”1.4,在这里下载它——或者更好地从svn repo下载代码——安装svn并运行svn co http://svn.pyinstaller.org/trunk pyinstaller。

As @Nicholas implies, dynamic libraries cannot be run from the same file as the rest of the executable -- but fortunately they can be packed together with all the rest in a "self-unpacking" executable that will unpack itself into some temporary directory as needed; PyInstaller does a good job at this (and at many other things -- py2exe is more popular, but pyinstaller in my opinion is preferable in all other respects).

正如@Nicholas所暗示的,动态库不能从与可执行文件的其他部分相同的文件中运行——但幸运的是,它们可以与所有其他部分打包在一个“自解包”可执行文件中,该可执行文件将自己解压缩到某个临时目录中;PyInstaller在这方面做得很好(在许多其他方面——py2exe更受欢迎,但在我看来,PyInstaller在其他所有方面都更可取)。

#5


6  

1) Get py2exe from here, according to your Python version.

从这里得到py2exe,根据您的Python版本。

2) Make a file called "setup.py" in the same folder as the script you want to convert, having the following code:

2)制作一个名为“setup”的文件。在您要转换的脚本所在的文件夹中,有以下代码:

from distutils.core import setup
import py2exe
setup(console=['myscript.py']) #change 'myscript' to your script

3) Go to command prompt, navigate to that folder, and type:

3)转到命令提示符,导航到该文件夹,输入:

python setup.py py2exe

4) It will generate a "dist" folder in the same folder as the script. This folder contains the .exe file.

4)它将在与脚本相同的文件夹中生成一个“dist”文件夹。此文件夹包含.exe文件。

#6


2  

you may want to see if your app can run under IronPython. If so, you can compile it to an exe http://www.codeplex.com/IronPython

您可能想看看您的应用程序是否可以在IronPython下运行。如果是这样,您可以将它编译为exe http://www.codeplex.com/IronPython

#7


0  

You could create an installer for you EXE file by:
1. Press WinKey + R
2. Type "iexpress" (without quotes) into the run window
3. Complete the wizard for creating the installation program.
4. Distribute the completed EXE.

您可以通过:1为您的EXE文件创建一个安装程序。按WinKey + r2。在运行窗口3中输入“iexpress”(不带引号)。完成创建安装程序的向导。4所示。EXE分配完成。