python py如何变成exe_如何将.py转换为.exe for Python?

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

downloading an old version of Python and using pyinstaller/py2exe

setting up a virtual environment in Python 3.6 that will allow me to do 1.

downloading a Python to C++ converter and using that.

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 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?

解决方案

Steps to convert .py to .exe in 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 named myfirstprog.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.

See the original script in my blog.

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 = "",

options = options,

version = "",

description = '',

executables = executables

)

EDIT:

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 = "", version = "0.11", description = '' )

the imported packages must be installed before you start step 8.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值