Use py2exe to convert a Python program to an exe file in Windows

Source: Internet
Author: User
Tags anzhuang
This article mainly introduces how to convert a Python program into an exe file using py2exe in Windows. Note that py2exe is only responsible for file format conversion and cannot compile a Python program as a machine code, for more information, see Prerequisites:
Install the easy-install module, which is a python module packaging tool.

First download the source code of easy_setup.py ,:
Http://pypi.python.org/pypi/setuptools
Use NotePad to store source code. py extension name, which can be executed on the command line, so that you can see several more files about easy_install In the python \ Scripts directory under the Python installation directory. This indicates that easy_install has been installed, how should I use this installation method?

Open cmd, run the command line, and go to the Python \ Scripts directory. Execute easy_install python-dateutil in this directory. you can install the dateutil module and install the pyparsing module.

My machine is:

E:\ANZHUANG\Python\Scripts>easy_install python-dateutil E:\ANZHUANG\Python\Scripts>easy_install pyparsing E:\ANZHUANG\Python\Scripts>easy_install chardet 

So the execution is OK!

Install the py2exe module:
Http://prdownloads.sourceforge.net/py2exedownload the corresponding installation package.

Sample Code:
Create a test. py file with the following content:

print "show me" 

Create a new mysetup. py compilation file with the following content:

From distutils. core import setup import py2exe setup (console = ["test. py"]) # note that test. py is consistent with the previous one.

Run the following command:

>>python mysetup.py py2exe

Running result:
A new dist directory under the current directory contains unequal files of the following types:

  • . Exe contains a directory file
  • . Pyd compiled py file
  • . Dll external DLL file, which contains a pythonxx. dll
  • The library files required for. zip are packed with all the compilation files for the library files.

View help:

python mysetup.py py2exe --help 

Example directory:
After the py2exe module is installed, the built-in samples are stored in: lib \ site-packages \ py2exe \ samples.

Note:
The parameters required for Compiling different types of binary are different. For example, the parameter name of the command line program is console, while that of the GUI program is Windows; for details, refer to the sample or HELP command script first!

Background:
I thought that only one exe file was left after the conversion. I used it to find that I didn't want to perform binary encoding for all the content as I imagined. Well, sometimes you think too well. After all, this is the easiest way to achieve it!

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.