Current location - Health Preservation Learning Network - Slimming men and women - Package it into EXE file with Pyinstaller.
Package it into EXE file with Pyinstaller.
Pyinstaller can package Python scripts into programs and run them directly under Windows, Linux and other operating systems. Python scripts can be run directly without installing Python, which is convenient.

python 2.7. 12 + Windows7

The absolute path of 1. The py file to be converted should not contain Chinese characters. It is easy to have some puzzling problems.

2.python needs one in. Py file. Otherwise, unexpected results will appear in the converted. Exe file.

1. Configure pip image source.

Create a pip directory and a pip.ini file under the user's home directory. The method is as follows

Step 1: Obtain the path corresponding to the HOMEPATH variable, and perform set HOMEPAT H or echo %HOMEPATH% acquisition.

The above results show that the path corresponding to the user's home directory HOMEPATH is C:\Users\Administrator.

Step 2: Create a pip directory under the directory C:\Users\Administrator, and create a pip. Ini file in PIP directory. If you already have a pip directory and a pip.ini file, please skip. In particular, the suffix of the pip.ini file is. So pay attention to the possible impact of hiding the extended file name. Add relevant content in pip.ini file and save it:

Step 3: Test the pip tool to confirm whether the third-party library can be automatically installed, such as using pip to automatically install the Pexpect class library.

Step 4: Enter the python tool and execute the import pexpect import module command. If there are no errors, pip will automatically install the Expect library successfully.

Step 5: After the installation is completed, you can directly execute pip install ${PackageName} to install the third-party library in any path.

2. Open the cmd command line window, enter pip install pyinstaller, and install the pyinstaller library.

3. Confirm the installation result of pyinstaller, which is located in the path of c:\Python27\Scripts. Execute where pyinstaller view.

Py installer[ options] script

Example: pyinstaller -F myscript.py

Options Description of common options:

Pyinstaller actually packages python interpreter and script into an executable file, which is completely different from compiling into real machine code. Therefore, packaging may not necessarily improve the running efficiency, but it may reduce the running efficiency, but the advantage is that python and script-dependent libraries are not installed on the running machine.

After the specified script enters, pyinstaller will first analyze other dependencies on which the script depends, then search for replication, collect all relevant dependencies and encrypt them, including python interpreter, and finally put these files into a directory or package them into an executable file, and then you can directly run the generated executable file.

It should be noted that the executable file packaged by pyinstaller can only run in the same environment as the packaging machine system. Programs encapsulated in 32-bit python environment can run on 32/64-bit windows system. Programs packaged in 64-bit python environment can only run on 64-bit windows system. So if you want to package the program, it is recommended to use a 32-bit python environment for packaging.

1. Confirm. The py file to be converted can run correctly without syntax errors. Such as ccc.py

2. Execute pyinstaller -F ${Python script name} to complete file conversion. The absolute path generated by. Exe files will be displayed in the penultimate line, usually in the directory where dist is located in the current directory. A convert. Exe file name is the same as python file name. As shown in the following figure

1. If a Python script uses a third-party library, how to package it?

Method 1: Copy the package corresponding to the third-party library to the same directory of the python script to be packaged, and then execute the packaging command.

Method 2: pyinstaller.exe-F path \ file name.pypath \ file name.py.

2. My python script is mainly output from the command line, but I can't view the relevant information after the program is executed. How to deal with it?

Add a command to the last line of the python script: os.system('pause') or raw _ input ('press enter to exit ...').

3. I want to change the icon of my packaged executive plan. What should I do?

Use the parameter -i. For example, the command: pyinstaller-f-i tupian \ qq.icoccc.py. The file suffix must be. ico。

4. The CMD window will appear when the program is running. How to remove it?

Just take the parameter-w.pyinstaller.exe-fcall _ login.py-w (-w means to remove the console window display).

5.pip configuration mirror source reference