Establish a virtual environment to solve the problem of python packaging exe file is too large.

Packaging generally uses the cmd command line, which requires cmd to execute python programs.
See the link for the tutorial:
cmd executes the python program, and the folder directly enters the cmd program
or packaged in the command line of anaconda.

Original packaging tutorial, use anaconda to
see the link:
python is packaged into an exe executable file. Tutorials

The packaged file is 238M. The reason is that anaconda contains too many packages that we don't need

Build a virtual environment package

Pipenv is a command line software for managing virtual environments. Simply put, it can create a local Python environment only in a certain directory, and this environment can be separated from the global environment.

Proceed as follows:

Step 1: Install Pipenv
Install Pipenv, here you can install it under Anaconda Powershell Prompt of Anaconda

pip install pipenv

Step 2: Enter the virtual environment

pipenv shell

It can be seen from the figure that there is still a difference between the CMD lines before and after entering the virtual environment. The second red circle in the virtual environment has a few more English letters than the first red circle.

Step 3: Install dependent libraries
. Install Pyinstaller and third-party libraries that your own scripts depend on in a virtual environment. For
example, mine is

pipenv install pyinstaller
pipenv install opencv-python

Forgot to take the screenshot before installing the library in this part, forgive me.

Step 4: Check the installed libraries.
If you use the pip list command and find that there are only a few libraries, it means that we have successfully entered the virtual environment.

The virtual environment should be able to delete the library. The instruction is similar to pipenv uninstall library

Step 5: Packaging
Before packaging, the program must be copied to the cmd project directory.
My project directory is ‪C:\Users\Shineion\
There are many packaging commands, such as packaging a single program, multiple programs, with icons...

simplest

pyinstaller -F xx.py

As a result, the exe in the dist file in the project directory is
only more than 50M this time, the original 238m

exe can run

Note: You can directly enter the next time you use the virtual environment. The packages installed in the virtual environment last time still exist, so there is no need to install some packages repeatedly

Appendix: common usage guide of pipenv

pipenv install: create a virtual environment
pipenv shell: enter the virtual environment (if it does not exist, create and enter the virtual environment)
pipenv install flask: install the module
pipenv uninstall flask: uninstall the module
pipenv graph: view the dependencies between the modules
pip list: view All modules of the virtual environment
exit(): exit the virtual environment
pip freeze> requirements.txt: export all dependent package names of the virtual environment
pip install -r requirements.txt: install all modules dependent on the project
pipenv uninstall --all: uninstall all packages
pipenv lock : Generate lockfile
pipenv --rm: Delete virtual environment
pipenv run python xxx.py: Run python in virtual environment

Insert picture description here
The new computer in electrical engineering: Yu Dengwu. Writing blog posts is not easy. If you think this article is useful to you, please give me a thumbs up and support, thank you.
Insert picture description here

Guess you like

Origin blog.csdn.net/kobeyu652453/article/details/108710837