Use Py2exe to package Python programs as EXE programs

Source: Internet
Author: User

Recently helped a friend to write a Python applet, from the Internet to crawl some needed articles to the local. In order to run conveniently, I hope to be able to convert EXE program under Windows regular execution. From Baidu to find some articles, found that the application of py2exe more, hence the use of.

1. Download Py2exe

Official website: http://www.py2exe.org/index.cgi/FrontPage

I'm using py2exe-0.6.9.win32-py2.7.exe .

can be installed directly.

After the installation is complete, the Python lib/site-packages directory will have more Py2exe directories and egg-info files.

2. Writing setup.py

Py2exe is very simple to use, after writing the setup.py file, execute python setup.py py2exe.

The simplest example can refer to the official website Tutorial Example: http://www.py2exe.org/index.cgi/Tutorial

Here my setup.py file is as follows:

#Coding=utf8 fromDistutils.coreImportSetupImportPy2exeextra_modules = ["BS4"]Options= {"Py2exe":                {"Compressed": 1,                 "optimize": 2,                 "Bundle_files": 1,#to package all the files into an EXE executable program                 "Packages": Extra_modules,                 #"includes": Extra_modules}}setup (Version="1.0.0", Description="test for Py2exe", name="py2exetest", Options=options, ZipFile=None, console= [{"Script":"app_main.py"}])

Several important parameters are:

console = [{"Script": "app_main.py"}] indicates that this is a console program and wants to package app_main.py into an EXE program. My project has multiple py source files, where only to specify the portal Py file, Py2exe will automatically import other py files.

ZipFile = none The default Py2exe generates a Library.zip library file, and none in this case will not generate a change file and package it into an EXE file.

"Packages": Extra_modules Specifies other modules that depend on. I used the BeautifulSoup, so here's the designation BS4. Here I use "includes": Extra_modules also no problem, there is no difference between the scrutiny.

"Bundle_files": 1 means to package all files into an EXE executable program

3. Generating EXE files

In the setup.py directory, execute the python setup.py py2exe command.

Successful execution will generate a build and dist two directories in the same directory. Build is an intermediate process performed by Py2exe and can be deleted. Dist is the generated EXE file, as follows:

Where App_main.exe is my program, execute it. W9xpopen.exe is py2exe automatically generated.

4. Questions

Packaging process encountered a problem "importerror:no module named BS4", later on-line search found that the problem is more common. But the process is still small and twists and turns, so record it.

phenomenon: Packaged into EXE The process was smooth, but the execution occurred Importerror problem, BS4 the module could not be found.

To find solutions on the Internet, there is an article that is required to display the specified import BS4 in app_main.py, otherwise py2exe will be "deceived", mistakenly think that the BS4 module will not be packaged. Try the method, the problem is still, not resolved.

After that, it is necessary to specify this additional dependency module in setup.py, "includes": ["BS4"]. Modified setup.py, not only used the includes parameter, but also used the packages parameter. New issues arise, and the "importerror:no module named BS4" issue occurs during the packaging process . The problem still cannot be solved.

Later find, see a netizen's reply, feel may be beautifulsoup installation Py2exe not recognize. I found the egg file installed under Python's lib/site-packages, Beautifulsoup4-4.4.0-py2.7.egg. Unzip it with WinRAR, put the BS4 directory directly under Lib/site-packages, then package and execute, everything is OK. Problem solving.

Ascan be seen, py2exe cannot recognize the egg file and need to be decompressed .

Use Py2exe to package Python programs as EXE programs

Related Article

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.