Recently, there is a need to make an interface and package it into executable programs for various platforms (win, android, linux, mac, etc.), first practice with basic windows.
How to write interface and cross-platform packaging, I will introduce it in my next blog post.
Introduction of applied technology
My environment is in the anaconda3 prompt, which is the environment in my previous blog post.
- There is nothing to pack with pyinstaller
- Use python virtual environment to package (the reason will be explained below)
- Some pits and problems encountered
Explain why it is packaged in a virtual environment
Because pycharm or your daily work python environment, there will basically be a lot of used and unused libraries, which will be packaged together when pyinstaller is packaged, resulting in basically a very simple py script, packaged out of 200M Above (I'm dropping mom)
To view the libraries of the current environment, the following commands can be used in anaconda:
conda list
Can see a lot. . . .
Therefore, we need to build a clean virtual environment, install only the libraries we need, and then package it with pyinstaller.
Enter the packaging process
First of all, use the following command to check the environment that has been created in anaconda:
conda env list
The above are the base (py3.7), py2 (py2.7), py36 (py3.6), nilmtk-env (charge decomposition environment) I usually use
Now we create a new virtual environment:
(The python version of the virtual environment, according to the file you want to package, I am 3.6 here)
(work_space is the name of the virtual environment, you can name it yourself)
conda create --name work_space python=3.6
Enter y to continue to install the python of the virtual environment. (Open vpn will be fast)
After the environment is created, use
conda env list
Let's take a look at our list of all environments.
You can see the name and directory of our new environment.
Then use the command:
(work_space is the name of the virtual environment you created)
activate work_space
To switch to the newly created virtual environment.
Next we install pyinstaller in this environment
pip install pyinstaller
Then use pip to install the libraries we need to compile the python file, for example, I installed the specified version of numpy and pyaudio
The environment is all set up, we can package it. The
packaging command refers to https://www.jianshu.com/p/48f6dea265eb and it is very clear.
My program is very simple, just a py script, displayed in the console, I only need to package it as an exe. (If you have a lot of scripts, please refer to the packaging instruction of the link above, let's associate py to package)
Switch to the py file directory to be packaged,
Then my command is
(package getChord_micphone.py as an .exe, and display the console)
pyinstaller -c -F getChord_micphone.py
A lot of English after execution
Finally, it will show the successful packaging and the path of the exe.
Take a look,
wow!
Only 18Mb! !
Are you super happy? ! !
Then one run. . puff. . . . The crash is over. . . . .
Don't panic~~, I will teach you how to read errors
Change the directory to the generated .exe directory and
use .\ to run this file.
For example, mine:
.\getChord_micphone.exe
Your own script didn’t use this library, right, he secretly used it and haven’t found it yet
So we block him when we pack
Come back to the directory of the python file to be packaged just now
You will find a lot of files in this place
We use a text editor or notepad or something to open the .spec file with the same name
Add the pkg_resources.py2_warn that was not found in the above error report to hiddenimports
.
Then switch to the directory here, and modify the previous packaging command to use this configuration file to package
(getChord_micphone.spec is your configuration file)
pyinstaller -c -F getChord_micphone.spec
It's a bunch of English again. . .
Wait patiently. . .
The packaging is successful again,
or the 18Mb
lightly double-click the generated exe
! ~~~
Finishing work
After the packaging is complete, if you need to use this virtual environment to package the same project, you can keep it.
If you don’t need this virtual environment,
conda env list
activate 你之前的虚拟环境名称
delete -n 要删除的虚拟环境名称 --all
It's OK.