Make python code to exe file

本文介绍如何通过py2exe将Python脚本转换为独立的Windows可执行文件。步骤包括下载安装py2exe、准备源代码和配置文件、使用命令行编译等。适用于希望发布跨平台应用的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Download and install py2exe from the py2exe website

(make sure you download the right version!)

2. Create a directory on you hardisk called mycode ... - Right click on the Windows Desktop and select New .. Folder and name it "mycode"

3. Double click the mycode folder to open it

4. Right click in the folder and select New.. Text Document, name it "hello.txt"

5. Right click in the folder and select New.. Text Document, name it "setup.txt"

6. Double click hello.txt to open it in Notepad

7. Copy the following text into the file and save it:

#Start here
print "Hello py2exe"
raw_input("Press any key to finish")
#End here

8. Double click the setup.txt and copy in the following code (and save the file):

#Start here
from distutils.core import setup
import py2exe

setup(console = ["hello.py"])
#End here

9. Right click on the hello.txt file and rename it to hello.py (ignore the warning)

10. Right click on the setup.txt file and rename it to setup.py (ignore the warning)

11. Start a command console (also called a DOS box) (It is found in the START-> Accessories menu on Windows XP). Alternatiively -start a command console by clicking Start -> Run...  and type cmd<enter> in the dialog.

12. In the console type

cd desktop<enter>

cd mycode<enter>

13. Type

start hello.py<enter>

14. If you entered the code correctly then a new console is created which asks you to press a key

15. The program works so now we can also use py2exe to make a standalone program. Type

start setup.py py2exe<enter>

16. If there are no errors in the setup.py file then py2exe will run. It creates a new sub-directory called dist. Type

cd dist<enter>

17. There should be a file called hello.exe Type:

hello<enter>

18. You have finished. Remember - every file in the dist directory is needed for your hello.exe to run properly. (Zip all the files together and send it to your friends).

 

If you write your own programs they probably won't be called hello.py .

a. Put your python file in the mycode directory

b. Edit setup.py and replace "hello.py" with the name of your own script.

c. Do step 15 above.

Thanks to those of you who made useful comments. 

Have fun! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值