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

发布于 2024-08-19 20:24:19 字数 976 浏览 14 评论 0原文

我编写了一个可以帮助 Windows 用户日常生活的脚本。我想简单地向她发送 .exe,而不是要求她安装 python、dll 或处理任何其他文件。

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

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

我调查了:

但我无法获取它们工作或无法理解如何获得我需要的结果。我得到的最接近的是 py2exe,但它仍然给了我 MSVCR71.dll

我希望得到一步一步的答案,因为我也无法遵循这里的一些调整答案,这些答案需要事先了解如何使用 py2exe 或其他一些工具。

我使用 Python 2.5,因为其中一个模块仅适用于该版本。

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.

I've read plenty of the stackoverflow 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.

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我的鱼塘能养鲲 2024-08-26 20:24:19

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

此处有一个简单的 PyInstaller 教程。如果您对使用有任何疑问,请留言...

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

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

最后的乘客 2024-08-26 20:24:19

使用 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,
)

然后您可以通过命令提示符/空闲运行它,两者都对我有用。希望有帮助

Using py2exe, include this in your 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

轮廓§ 2024-08-26 20:24:19

我建议去 http://sourceforge.net/projects/py2exe/ files/latest/download?source=files 下载 py2exe。然后创建一个名为 setup.py 的 python 文件。
在其中输入“

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

保存在您的用户文件夹中”
还将要转换的文件保存在同一文件夹中

运行窗口的命令提示符
输入 setup.py install py2exe

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

接下来,打开 dist 文件夹。

运行exe 文件。

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

夹复制/发送 dist 文件夹给人员。

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

希望它有效!:)

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

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

It should print many lines of code...

Next, open the dist folder.

Run the exe file.

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

Copy/Send the dist folder to person.

Optional: Change the name of the dist folder

Hope it works!:)

愁杀 2024-08-26 20:24:19

我会和 @Nicholas 一起推荐 PyInstaller (带有 --onefile 标志),但请注意不要使用“最新版本”PyInstaller 1.3 - 它已经多年了。使用“预发行版”1.4,此处下载 - 或者更好的是 svn repo 中的代码 - 安装 SVN 并运行 svn co http://svn.pyinstaller.org/trunk pyinstaller 。

正如@Nicholas 所暗示的那样,动态库不能从与可执行文件的其余部分相同的文件中运行 - 但幸运的是,它们可以与所有其余部分打包在一起自解包”可执行文件将根据需要将自身解压到某个临时目录中; PyInstaller 在这方面做得很好(在许多其他方面 - py2exe 更受欢迎,但我认为 pyinstaller 在所有其他方面都更可取)。

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.

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).

别靠近我心 2024-08-26 20:24:19

1) 根据您的Python版本,从此处获取py2exe。

2)在与要转换的脚本相同的文件夹中创建一个名为“setup.py”的文件,其中包含以下代码:

来自 distutils.core 导入设置
导入py2exe
setup(console=['myscript.py']) #将 'myscript' 更改为您的脚本

3) 转到命令提示符,导航到该文件夹​​,然后键入:

python setup.py py2exe

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

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

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

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:

python setup.py py2exe

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

四叶草在未来唯美盛开 2024-08-26 20:24:19

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

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

此生挚爱伱 2024-08-26 20:24:19

您可以使用 NSIS (Nullsoft 可编写脚本的安装系统)从 python 脚本创建可执行文件。按照以下步骤将 python 文件转换为可执行文件。

  • 在您的系统中下载并安装NSIS

  • 压缩 .zip 文件中要导出到可执行文件中的文件夹。

  • 启动NSIS并选择基于ZIP文件的安装程序。查找并提供压缩文件的路径。

  • 提供您的安装程序名称默认文件夹路径,然后单击生成以生成您的exe文件。< /p>

  • 完成后,您可以单击测试来测试可执行文件或关闭来完成该过程。

  • 生成的可执行文件可以安装在系统上,并且可以分发以使用此应用程序,甚至无需担心安装所需的 python 及其包。

有关视频教程,请遵循:如何将任何 Python 文件转换为 .EXE

You can create executable from python script using NSIS (Nullsoft scriptable install system). Follow the below steps to convert your python files to executable.

  • Download and install NSIS in your system.

  • Compress the folder in the .zip file that you want to export into the executable.

  • Start NSIS and select Installer based on ZIP file. Find and provide a path to your compressed file.

  • Provide your Installer Name and Default Folder path and click on Generate to generate your exe file.

  • Once its done you can click on Test to test executable or Close to complete the process.

  • The executable generated can be installed on the system and can be distributed to use this application without even worrying about installing the required python and its packages.

For a video tutorial follow: How to Convert any Python File to .EXE

自演自醉 2024-08-26 20:24:19

您可以通过以下方式为 EXE 文件创建安装程序:
1. 按Win键+R
2. 在运行窗口中输入“iexpress”(不带引号)
3. 完成创建安装程序的向导。
4. 分发完成的EXE。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文