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

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

  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 中的每个 imported 包包含到 packages 列表中(例如:packages = ["idna", "os","sys"])
  • any name, any number, any descriptionsetup.py 文件中不应保持不变,您应该相应地更改它(例如:name = "<first_ever>", version = "0.11", description = '' )
  • 在开始步骤 8 之前,必须安装 imported 包.
  • 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 转换为 Python 的 .exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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