"01" Python packaged output as an. exe executable file

Source: Internet
Author: User
Tags file copy

this is my No. 01 blog post.python packaged output as an. exe executable file

After the completion of the crawler, in order to give the computer no Python environment of friends to play my crawler, began to try to export the crawler's. py file as an. exe executable.

First, the Python tutorial mentions the Py2exe module. But a wave of search later found that this thing only support to Python3.4, and I use Python3.5.2, which makes me very embarrassed ... Then went on a wave of search, found a module called Pyinstaller. This module can perfectly support the Python3.5 and then rage in.

The first is the installation. Go to the SourceForge on a great effort to download the zip file down and die do not know how to install .... [cover face] for this does not have EXE installation of things, English documents are obviously too lazy to check, and the online course through the CMD interface to operate, there is said that after the decompression even installed, but is not able to use ... Maybe I'm not in the right position ...

Then change the method. It was here that I realized that Windows can also be used to install programs on the command line as easily as Linux. If your computer already has Python installed and has a PIP, you can open cmd and type the following command:

Pip Install Pyinstaller

However, if it is a domestic network, this method can be ignored. Must be an error. Please follow this instruction after the science is online. If you have an error with permissions, try running cmd with administrator privileges.

Then you can install the module very, very readily, and will automatically help you to install the Pywin32 module (if not), and even the installation will prompt you to update the PIP (if the version is too old) ..... It's so conscience. The only drawback is that you can't use it without a ladder.

After installation, you can use this module happy to compile your py into EXE to the small partners to install the Force!! Hhhhh

Yet it is not so simple. There are two ways to compile: command-line mode and direct Python file invocation methods.

The first is the command line method:

Enter the directory where your py file is located under the command line (enter the other drive letter x:, for example D:; then CD x:\ ...). Your directory)

Then enter the following command: (***.py is the file name you want to compile)

Pyinstaller ***.py

Then wait to compile, in this directory will be reborn into four things, a ***.spec file (* * * and your py file name), a __pycache__ folder, a dist folder and a build folder. The exe you want is in the Dist folder. However, when copying to the small partners remember to copy the entire folder to them, this whole folder is part of the EXE. (The other is the compilation process of intermediate files, the output can be deleted)

You must think, so many things really trouble ah, how to output an EXE? That will add a single file parameter-F, as follows:

(In fact, there are many parameters available, in the law of the two introduced)

Pyinstaller-f ***.py

And then you will be happy to find that this time dist under only an EXE hahaha can be a happy outfit forced wwwww

でも!! There is a more convenient way!

Direct Python file invocation method:

Calling this module directly in Python is more convenient than CMD, at least I think. At least, in the future to output as long as the file copy a wave, change one or two parameters on it, it's cool.

Here's how:

First, create a py file in the same directory as the ***.py that you want to compile: targetpy2exe.py (of course, any other name can be XJB)

Then, write the following code in it:

1 #Code by Stevehawk2 #email [email protected]3 4  fromPyinstaller.__main__ ImportRun5 if __name__=='__main__':6opts = ['***.py','- F'R'--distpath=**'R'--workpath=**'R'--specpath=**'R'--icon=**']7Run (opts)

After adjusting the parameters run, you can output exe.

In the code, the list behind opts= is a series of parameters, explained below:

The First * * *. PY is the name of the file you want to compile, required [all parameters are optional]

The second- F is the parameter that generates the single file

The third --distpath=** means the path to the Dist folder (the location of the last output file), * * For the path, such as D:\My programs\python\ output \dist, The Dist folder in the current directory is assumed to be

  The fourth --workpath=** means the path to the build folder (temporary file), * * For the path, such as D:\My programs\python\ output \build, Default in the Build folder under the current directory

The fifth --specpath=** means the path of the ***.spec file (temporary file), * * For the path, such as D:\My programs\python\ output \, default to the current directory

The sixth --icon=** meaning is the output EXE file icon path,* * for the path , such as D:\My Programs\python\icon.ico

  For icons, it is best to create your desired ico icon file online on a website called Png2ico, and note that a JPG image file is renamed directly to the. ico will error [I was smart at first to find the problem after a long time. (cover face)]. When converting a picture on a png2ico, size remembers auto, so the ICO file generated will have four sizes of icons built into the needs of the system to scale.

Note that I added R to the quotation marks in the latter four arguments, because there are a variety of problems with no r, such as the escape character caused by the backslash, and the Chinese in some Chinese directory names will become a 16 Utf-8 code to show the resulting path error. Putting R (Raw) in front of these strings to represent the original string will solve these problems.

& 64-bit problem

In addition, there are 32-bit and 64-bit questions about the output EXE file. The problem is that the 64-bit Python environment is installed on my computer, and the exe file output by Pyinstaller is 64-bit. However, the small partner's computer is 32-bit, so it is not able to run properly. It's embarrassing for me ... It's not a good outfit. So how to output 32-bit EXE? Actually very simple, your Python environment is 32 bits, the output EXE will be 32 bits, 64 bits will output 64 bits. If you want to output both 32-bit and 64-bit output, here's how:

First, install the 32-bit and 64-bit Python environments separately and install the Pyinstaller separately. Remember, install an environment immediately with PIP install Pyinstaller installed Pyinstaller. If you have installed two environments and then installed Pyinstaller, one environment will succeed, but running again will prompt you to install and another environment will not install successfully. After the success of the two environments installed Pyinstaller, if you use the CMD command line to invoke the problem will occur, at least my computer will have this problem. At the moment I don't know how to invoke the two environments separately under CMD. The way python files are called at this time is much more convenient. In the case of the pycharm I use, it is clear that the ***.py and targetpy2exe.py to be compiled are under the same project. At this point, under the File tab, open the Setting option, find the project:*** (project name) option in the left column of the setting window, click the gray triangle on the left to open the drop-down menu, click the Project Interpreter option, Then select the desired 32-bit or 64-bit interpreter in the drop-down menu in the right-hand window. Select the following to click OK. You may need to wait a while for the first time to run. Then run targetpy2exe.py to generate the 32/64-bit EXE you want!!

But that's just the way it works for people of my type of obsessive-compulsive disorder. If wearies, only need to install a 32-bit Python environment, because 64-bit system can be compatible with 32-bit programs ..... This is the simplest solution.

Above.

"01" Python packaged output as an. exe executable file

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.