Package the Python3 file as an EXE file

Source: Internet
Author: User

How do we use Python to write code that can be used directly without the Python environment being configured? Especially for Windows audiences.

Because Python is an explanatory language, Python cannot execute without the Python interpreter. But we have a solution, and the answer is to package it as an. exe executable

of course, the text before the two nonsense, one is to see some of the network before The Spit groove, because did not learn the principle of compiling, so also can not understand the chaos, let's call it a semi-quote:

Python philosophy is WYSIWYG, scripting language is also doomed to its encryption is not as good as other compiled language (such as c++,java,c#), even if encapsulated as EXE, the source code costs a little effort, is almost equal to the visible, so, packaging Python for EXE more for ease of use, Want to keep the source for their own efforts, or try other languages, or purely by selling services (cloud Platform, web framework, do not need to see the source code on the client use)

Second, the whole groping process is extremely messy, related tutorials online is also relatively small, so I estimate some places, I said not too much, also please refer to the official documents (although self-feeling by the official document pits)

Here only to introduce the solution under Python3,windows7, Python2 of the relevant programs are very many, please explore yourself

First is the installation of Python3, go to the official website, my is 3.4 version of, unfortunately, many of the tutorials are for the 3.3 version.

But I can be very responsible for saying that the personal situation is under 3.4 cxfreeze.bat I can't find it in the Script folder, and when I install it, it comes out of the air. Not a batch command prompt

I really want to say, see DOS Pop this time, really want to vomit blood, really heart 10,000 grass mud horse Pentium and over

But the problem has to be solved, then how to do it?

First you have to let DOS know the Python command, how to do it?

System variables, advanced system settings, System Properties, computer---environment variables

The path contains the directory where your Python3 is located, and is separated from each other; Of course, it can be set in DOS, but DOS inside settings feel more trouble than GUI Setup

Mine is like this:

Then the protagonist appeared, Cx_freeze, help us to package Python3 file as exe file tool, first, to download down with, resources Mount SourceForge on

http://sourceforge.net/projects/cx-freeze/files/

Choose a suitable download for your own machine, I downloaded the latest version of the WINDOWS64-bit Edition

Then click to open the installation, because the next good variety can not find bat toss a half day.

I think it's better to see these two pages useful, official examples (Http://t.cn/REli4pL) and how to use them (Http://t.cn/REliK8R)

It is nice to have this tool (currently) does not appear in the Chinese language support is not good example, I also put in my own example

Put your source code and setup.py in the same path, CD to the corresponding path, execute the following command

Wait patiently, this tool will help you to pack the necessary libraries, of course, can not play third-party library not tested (should be able)

Put the corresponding code I set

hello.py

# -*-coding:utf-8-*- Import   while True:   print(" Show once a minute ")   Time.sleep  #  Delay for 1 minute (seconds)

setup.py

 from  cx_freeze import   Setup, executableexecutables  = [executable ( "  hello.py   " )]setup (name  = "  Hello   " , version  ="  0.1   "  = " sample CX _freeze script   " , executables  =< Span style= "COLOR: #000000" >executables)  

Certainly can also be advanced settings, I this is just the simplest configuration, point open exe directly with that, such as the. ico parameter settings, you should be able to set the icon of the exe you have created

If you think exe jump off, print information can not see, like my code example inside, import time module, plus sleep method, delay can be ~

Reprint: http://jimmy66.com/1102.html

Python Learning Exchange Group: 238757010

Package the Python3 file as an EXE file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.