如何将.py转换为.exe for Python? [英] How can I convert a .py to .exe for Python?

查看:88
本文介绍了如何将.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

  1. 下载旧版本的Python并使用pyinstaller/py2exe
  2. 在Python 3.6中设置虚拟环境,这将允许我执行1.
  3. 下载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的步骤

  1. 安装 Python 3.6 .
  2. 安装cx_Freeze,(打开命令提示符并键入pip install cx_Freeze.
  3. 安装idna,(打开命令提示符并键入pip install idna.
  4. 编写一个名为myfirstprog.py.py程序.
  5. 在脚本的当前目录上创建一个名为setup.py的新python文件.
  6. setup.py文件中,复制以下代码并保存.
  7. 在按住shift键的同时右键单击同一目录,因此您可以打开命令提示符窗口.
  8. 在提示符下,键入python setup.py build
  9. 如果您的脚本没有错误,那么在创建应用程序时将没有问题.
  10. 检查新创建的文件夹build.它具有另一个文件夹.在该文件夹中,您可以找到您的应用程序.运行.让自己开心.
  1. Install Python 3.6.
  2. Install cx_Freeze, (open your command prompt and type pip install cx_Freeze.
  3. Install idna, (open your command prompt and type pip install idna.
  4. Write a .py program named myfirstprog.py.
  5. Create a new python file named setup.py on the current directory of your script.
  6. In the setup.py file, copy the code below and save it.
  7. With shift pressed right click on the same directory, so you are able to open a command prompt window.
  8. In the prompt, type python setup.py build
  9. If your script is error free, then there will be no problem on creating application.
  10. 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"])
  • setup.py文件中的
  • any name, any number, any description不应保持不变,您应相应地对其进行更改(例如:name = "<first_ever>", version = "0.11", description = '')
  • import版本的软件包必须先安装,然后再开始步骤8 .
  • be sure that instead of myfirstprog.py you should put your .pyextension file name as created in step 4;
  • you should include each imported package in your .py into packages list (ex: packages = ["idna", "os","sys"])
  • any name, any number, any description in setup.py file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = '' )
  • the imported packages must be installed before you start step 8.

这篇关于如何将.py转换为.exe for Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆