[Python] Use pipenv to create a virtual environment for packaging


Insert image description here

Cause: There are too many modules installed locally. Using pyinstaller to package will package many irrelevant modules, making the program very large.

It would be nice if you could create a virtual python environment and only install the dependency packages of the program to be packaged.

Pipenv can exactly solve this problem. The dependency packages installed by pipenv will be saved in the project virtual environment directory and will not pollute the global system environment.

Of course, the application scenarios of pipenv don’t stop there.

1. Introduction to pipenv

Pipenv is a Python package management tool. It is the work of requests author Kenneth Reitz. It provides management between various versions of Python and various package management.

pipenv mainly has the following features:

  • Pipenv integrates the functions of pip and virtualenv.

  • pipenv will create a Pipfile file in the project root directory to record the version information of the package, and a Pipfile.lock file to lock the version and dependency information of the installation package and manage the dependencies between packages.

  • There is no need to activate the virtual environment to execute the code. As long as there is a pipfile file, you can use the dependency package of the virtual environment to execute the python script.
    For example: by executing the command pipenv run python xx.py

  • Execute pipenv install in a new project root directory, it will automatically create a virtual environment and generate a Pipfile file;
    When the install command does not pass parameters to specify the installation package , if Pipfile currently exists, all packages in the Pipfile will be automatically installed.

2. Quickly get started using pipenv

The following is a quick introduction to the basic use of pipenv. This article uses Python3.9 under windows.

2.1 Install pipenv

In the cmd form, enter the code:

pip install pipenv

After the installation is completed, create a new folder TEST2 at will, and switch the cmd administrator rights to the folder path.

2.2 Create a virtual environment

Method 1: Install the virtual environment of the specified python version: pipenv install --python 3.9

Note: There are two horizontal bars in front of –python. The .py project file to be packaged uses whichever Python version and third-party library is installed, whichever Python version is installed (the third-party library with the same name installed in different Python versions may be different), otherwise the packaged exe may not run.

Method 2: Directly use the commandpipenv install to create a new virtual environment in the virtual environment directory
Insert image description here

After is executed, you can see that the Pipfile and Pipfile.lock files are created in the current directory.
The Pipfile file is used to record the version information of the package;
The Pipfile.lock file is used to lock the version and dependency information of the installation package and manage the dependencies between packages;

Insert image description here

The Pipfile file can be copied to other projects and the virtual environment and dependency package installation can be generated based on this Pipfile file by executing the pipenv install command.

2.3 Activating the environment

Activate the environment in the root of the folder TEST2:pipenv shell

Check what modules are installed in the virtual environment:pip list
Insert image description here

2.4 Install project dependency packages in the virtual environment

Installing the required third-party libraries in the activated virtual environment is the same as installing libraries normally in Python, for example:pip install datetime

me

Uninstalling the library is the same as uninstalling the library normally in Python:pip uninstall datetime

Insert image description here

2.5 Check whether the project can run normally in the virtual environment

Put the project file to be run in the root directory of the folder TEST2. There are two ways to run it:
Method 1: In the activated In the virtual environment, cmd input:python xxx.py

Method 2: In the inactive virtual environment state, enter cmd in the root directory of the folder TEST2:pipenv run python xxx.py (as long as there is a Pipfile file Yes)

If no error is reported after running the program, the project is considered to be able to run normally in the virtual environment;
If the program reports an error and missing dependency packages, follow the steps in 2.4 above to install the project dependency packages.

2.6 Packaging projects

Install the packaging tool in the virtual environment:pip install pyinstaller
Packaging:pyinstaller -F xxx.py, other parameters of pyinstaller will not be introduced in detail.

If you want to enter the virtual environment next time, directly execute the cmd command in the root directory of the folder TEST2pipenv shell to enter

2.7 Delete virtual environment

Execute the command in cmdpipenv --rm
Insert image description here
After deleting the virtual environment, if the Pipfile and Pipfile.lock files still exist in the directory, you can reinstall the virtual environment through pipenv install, and the reinstalled virtual environment The environment and name are the same as before deletion.

If you want to exit the virtual environment, enter exit to exit
Insert image description here

The above is the entire process of using pipenv to create a virtual environment for packaging.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_41273999/article/details/134809079