如何将 .py 转换为 Python 的 .exe?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41570359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert a .py to .exe for Python?
提问by user7396807
I'm trying to convert a fairly simple Python program to an executable and couldn't find what I was looking for, so I have a few questions (I'm running Python3.6):
我正在尝试将一个相当简单的 Python 程序转换为可执行文件,但找不到我要找的内容,所以我有几个问题(我正在运行 Python3.6):
The methods of doing this that I have found so far are as follows
到目前为止,我发现的这样做的方法如下
- downloading an old version of Python and using
pyinstaller/py2exe
- setting up a virtual environment in 3.6 that will allow me to do 1.
- downloading a Python to C++ converter and using that.
- 下载旧版本的 Python 并使用
pyinstaller/py2exe
- 在 3.6 中设置一个虚拟环境,这将允许我做 1。
- 下载 Python 到 C++ 转换器并使用它。
Here is what I've tried/what problems I've run into.
这是我尝试过的/遇到的问题。
- I installed
pyinstaller
before the required download before it (pypi-something) so it did not work. After downloading the prerequisite file,pyinstaller
still does not recognize it. - If I'm setting up a virtualenv in 2.7, do I actually need to have 2.7 installed?
- similarly, the only python to C++ converters I see work only up until python 3.5 - do I need to download and use this version if attempting this?
- 我
pyinstaller
在它之前的所需下载之前安装了它(pypi-something)所以它不起作用。下载了先决条件文件后,pyinstaller
仍然无法识别。 - 如果我在 2.7 中设置 virtualenv,我真的需要安装 2.7 吗?
- 同样,我看到的唯一一个 python 到 C++ 转换器只能在 python 3.5 之前工作 - 如果尝试这个,我需要下载并使用这个版本吗?
回答by Maria Irudaya Regilan J
Steps to convert .py to .exe in Python 3.6
在 Python 3.6 中将 .py 转换为 .exe 的步骤
- Install Python 3.6.
- Install cx_Freeze, (open your command prompt and type
pip install cx_Freeze
. - Install idna, (open your command prompt and type
pip install idna
. - Write a
.py
program namedmyfirstprog.py
. - Create a new python file named
setup.py
on the current directory of your script. - In the
setup.py
file, copy the code below and save it. - With shift pressed right click on the same directory, so you are able to open a command prompt window.
- In the prompt, type
python setup.py build
- If your script is error free, then there will be no problem on creating application.
- Check the newly created folder
build
. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
- 安装Python 3.6。
- 安装 cx_Freeze,(打开命令提示符并键入
pip install cx_Freeze
. - 安装 idna,(打开命令提示符并输入
pip install idna
. - 编写一个
.py
名为的程序myfirstprog.py
。 - 创建一个以
setup.py
脚本当前目录命名的新 python 文件。 - 在
setup.py
文件中,复制下面的代码并保存。 - 按住 shift 右键单击同一目录,这样您就可以打开命令提示符窗口。
- 在提示中,键入
python setup.py build
- 如果您的脚本没有错误,那么创建应用程序就没有问题。
- 检查新创建的文件夹
build
。它还有另一个文件夹。在该文件夹中,您可以找到您的应用程序。运行。让自己开心。
See the original script in my blog.
请参阅我的博客中的原始脚本。
setup.py:
设置.py:
from cx_Freeze import setup, Executable
base = None
executables = [Executable("myfirstprog.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "<any name>",
options = options,
version = "<any number>",
description = '<any description>',
executables = executables
)
EDIT:
编辑:
- be sure that instead of
myfirstprog.py
you should put your.py
extension file name as created in step 4; - you should include each
import
ed package in your.py
intopackages
list (ex:packages = ["idna", "os","sys"]
) any name, any number, any description
insetup.py
file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = ''
)- the
import
ed packages must be installed before you start step 8.
- 确保
myfirstprog.py
您应该输入.py
在步骤 4 中创建的扩展文件名; - 你应该包括每
import
版包您.py
到packages
列表(例如:packages = ["idna", "os","sys"]
) any name, any number, any description
在setup.py
文件不应保持不变,就应该相应地改变它(例如:name = "<first_ever>", version = "0.11", description = ''
)- 在
import
开始第 8 步之前,必须安装 ed 软件包。
回答by Rodrigo Nascimento
Python 3.6 is supported by Pyinstaller.
Pyinstaller 支持 Python 3.6。
Open a cmd window in your Python folder (open a command window and use cd
or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter
在您的 Python 文件夹中打开一个 cmd 窗口(打开一个命令窗口并使用cd
或在按住 shift 的同时,在 Windows 资源管理器上右键单击它并选择“在此处打开命令窗口”)。然后只需输入
pip install pyinstaller
And that's it.
就是这样。
The simplest way to use it is by entering on your command prompt
使用它的最简单方法是在命令提示符下输入
pyinstaller file_name.py
For more details on how to use it, take a look at this question.
有关如何使用它的更多详细信息,请查看此问题。
回答by Gab
There is an open source project called auto-py-to-exe on Github. Actually it also just uses Pyinstaller internally but since it is has a simple GUI that controls Pyinstaller it may be a comfortable alternative. It can also output a standalone file in contrast to other solutions. They also provide a videoshowing how to set it up.
Github 上有一个名为 auto-py-to-exe 的开源项目。实际上它也只是在内部使用 Pyinstaller,但由于它有一个简单的 GUI 来控制 Pyinstaller,因此它可能是一个舒适的选择。与其他解决方案相比,它还可以输出独立文件。他们还提供了一个视频,展示了如何设置它。
GUI:
图形用户界面:
Output:
输出:
回答by Gerschel
I can't tell you what's best, but a tool I have used with success in the past was cx_freeze. They recently updated (on Jan. 7, '17) to version 5.0.1 and it supports Python 3.6.
我不能告诉你什么是最好的,但我过去成功使用的一个工具是 cx_freeze。他们最近(2017 年 1 月 7 日)更新到了 5.0.1 版,它支持 Python 3.6。
Here's the pypi https://pypi.python.org/pypi/cx_Freeze
这是 pypi https://pypi.python.org/pypi/cx_Freeze
Docs show that there is more than one way to do it depending on your needs.
http://cx-freeze.readthedocs.io/en/latest/overview.html
文档表明,根据您的需要,有不止一种方法可以做到这一点。
http://cx-freeze.readthedocs.io/en/latest/overview.html
I have not tried it out yet, so I'm going to point to a post where the simple way of doing it was discussed. Some things may or may not have changed though.
How do I use cx_freeze?
我还没有尝试过,所以我将指向一篇讨论简单方法的帖子。不过,有些事情可能已经改变,也可能没有改变。
如何使用 cx_freeze?
回答by MikeyB
I've been using Nuitka and PyInstaller with my package, PySimpleGUI.
我一直在使用 Nuitka 和 PyInstaller 和我的包 PySimpleGUI。
NuitkaThere were issues getting tkinter to compile with Nuikta. One of the project contributors developed a script that fixed the problem.
Nuitka使用 Nuikta 编译 tkinter 时出现问题。其中一位项目贡献者开发了一个解决问题的脚本。
If you're not using tkinter it may "just work" for you. If you are using tkinter say so and I'll try to get the script and instructions published.
如果您不使用 tkinter,它可能“对您有用”。如果您使用的是 tkinter,请说出来,我会尝试发布脚本和说明。
PyInstallerI'm running 3.6 and PyInstaller is working great! The command I use to create my exe file is:
PyInstaller我正在运行 3.6 并且 PyInstaller 运行良好!我用来创建我的 exe 文件的命令是:
pyinstaller -wF myfile.py
pyinstaller -wF myfile.py
The -wF will create a single EXE file. Because all of my programs have a GUI and I do not want to command window to show, the -w option will hide the command window.
-wF 将创建单个 EXE 文件。因为我的所有程序都有一个 GUI 并且我不想显示命令窗口,所以 -w 选项将隐藏命令窗口。
This is as close to getting what looks likea Winforms program to run that was written in Python.
这是接近得到什么看起来像一个WinForms程序来运行这是用Python编写的。
[Update 20-Jul-2019]
[2019 年 7 月 20 日更新]
There is PySimpleGUI GUI based solution that uses PyInstaller. It uses PySimpleGUI. It's called pysimplegui-exemakerand can be pip installed.
有使用 PyInstaller 的基于 PySimpleGUI GUI 的解决方案。它使用 PySimpleGUI。它被称为pysimplegui-exemaker,可以通过 pip 安装。
pip install PySimpleGUI-exemaker
pip install PySimpleGUI-exemaker
To run it after installing:
安装后运行:
python -m pysimplegui-exemaker.pysimplegui-exemaker
python -m pysimplegui-exemaker.pysimplegui-exemaker
回答by Tejas Joshi
py2exe is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts; Python 3.3 and later are supported. It can build console executables, windows (GUI) executables, windows services, and DLL/EXE COM servers.
py2exe 是一个 distutils 扩展,它允许从 Python 脚本构建独立的 Windows 可执行程序(32 位和 64 位);支持 Python 3.3 及更高版本。它可以构建控制台可执行文件、Windows (GUI) 可执行文件、Windows 服务和 DLL/EXE COM 服务器。
You can download it here:
您可以在这里下载: