如何将 .py 转换为 Python 的 .exe? [英] How can I convert a .py to .exe for Python?
本文介绍了如何将 .py 转换为 Python 的 .exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将一个相当简单的 Python 程序转换为可执行文件,但找不到我要找的内容,所以我有几个问题(我正在运行 Python 3.6):
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 Python 3.6):
目前我发现的实现方法如下
The methods of doing this that I have found so far are as follows
- 下载旧版本的 Python 并使用
pyinstaller/py2exe
- 在 Python 3.6 中设置一个虚拟环境,让我可以做 1.
- 下载 Python 到 C++ 转换器并使用它.
这是我尝试过的/遇到的问题.
Here is what I've tried/what problems I've run into.
- 我在下载之前安装了
pyinstaller
(pypi-something),所以它不起作用.下载必备文件后,pyinstaller
仍然无法识别. - 如果我在 Python 2.7 中设置 virtualenv,我真的需要安装 Python 2.7 吗?
- 同样,我看到的唯一一个 Python 到 C++ 转换器只能在 Python 3.5 之前工作 - 如果尝试这样做,我是否需要下载并使用此版本?
- 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 Python 2.7, do I actually need to have Python 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?
推荐答案
在 Python 3.6 中将 .py 转换为 .exe 的步骤
- 安装 Python 3.6.
- 安装 cx_Freeze,(打开命令提示符并输入
pip install cx_Freeze
. - 安装 idna,(打开命令提示符并输入
pip install idna
. - 编写一个名为
myfirstprog.py
的.py
程序. - 在脚本的当前目录中创建一个名为
setup.py
的新 python 文件. - 在
setup.py
文件中,复制下面的代码并保存. - 按住 shift 键,右键单击同一目录,这样您就可以打开命令提示符窗口.
- 在提示中,输入
python setup.py build
- 如果您的脚本没有错误,那么创建应用程序就没有问题.
- 检查新创建的文件夹
build
.它还有另一个文件夹.在该文件夹中,您可以找到您的应用程序.运行.让自己开心.
- 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.
在我的博客.
setup.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
)
- 确保您应该输入在步骤 4 中创建的
.py
扩展文件名,而不是myfirstprog.py
; - 您应该将
.py
中的每个import
ed 包包含到packages
列表中(例如:packages = ["idna", "os","sys"]
) any name, any number, any description
在setup.py
文件中不应保持不变,您应该相应地更改它(例如:name = "<first_ever>", version = "0.11", description = ''
)- 在开始步骤 8 之前,必须安装
import
ed 包.
- 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.
这篇关于如何将 .py 转换为 Python 的 .exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文