如何将.py转换为.exe for Python? [英] How can I convert a .py to .exe for Python?
本文介绍了如何将.py转换为.exe for Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将一个相当简单的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
不应保持不变,您应相应地对其进行更改(例如:name = "<first_ever>", version = "0.11", description = ''
) -
import
版本的软件包必须先安装,然后再开始步骤8 .
setup.py
文件中的- 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转换为.exe for Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文