The whole process of managing python virtual environment with pipenv

  • 2021-12-04 10:44:43
  • OfStack

pipenv is the work of Kenneth Reitz, which can effectively manage multiple environments and various packages of Python. In the past, we used virtualenv to build a virtual environment and manage python version, but the use of cross-platform is not very good, and sometimes there are always problems in dealing with the dependencies between packages; In the past, pip was often used for packet management. pip is good enough, but pipenv is still recommended, which is equivalent to the combination of virtualenv and pip and is more powerful. After pipenv was opened source, it became very popular on GitHub (up to now, there are more than 9600 stars).

pipenv mainly has the following features:

(1) pipenv integrates the functions of pip and virtualenv, and perfects some defects of them.

(2) In the past, there may be problems in managing requirements. txt files with virtualenv. Pipenv uses Pipfile and Pipfile. lock, which stores the dependencies of packages. It is 10 points convenient to view the dependencies.

(3) Hash check is used in various places, and no matter whether the package is installed or uninstalled, it is 10 points safe, and it will automatically disclose security vulnerabilities. .

(4) Simplify the development workflow by loading the. env file.

(5) Support Python2 and Python3, and the commands on each platform are 1.

The following describes the use of pipenv management python virtual environment, the whole process, a look at it!

python The virtual environment can create a separate environment for the project, which can solve the trouble that using different versions of dependencies brings conflicts to the project. There are many ways to create a virtual environment, pipenv Will automatically help you manage the virtual environment and dependency files, and provides 1 series of commands and options to help you implement various dependency and environment management related operations.

1. Install pipenv

pip install pipenv

2. Pipfile files and Pipfile. lock files

Pipfile Documents and Pipfile.lock Is generated when creating a virtual environment to record information about installation dependencies, Pipfile When used to replace pip Adj. requirements.txt Of.

Pipfile.lock Contains your system information, dependencies and version information for all installed packages, and hash validation information for all installed packages and their dependencies.

3. Create a virtual environment

$ cd myproject

//Create a virtual environment for python 3.7
$ pipenv --python 3.7

Creating a virtual environment will find whether the current directory exists by default .venv Directory, if not, it will default under the home directory user /Users/xxxx/.local/share/virtualenvs Create a virtual environment directory. It is recommended to create 1 under the project first .venv Folder, and then create a virtual environment.

If an pipfile does not exist, an piffle is generated, and if any libraries are added, the file is automatically edited.

4. Activate/Enter pipenv shell

$ pipenv shell
$ python --version

//Dependent installation based on Pipfile, ignoring Pipfile. lock
$ pipenv install --skip-lock

//Install the specified version of the module
$ pip install pymongo==xxx

According to Pipfile Conduct dependency package installation.

Step 5 Exit the virtual environment

$exit or ctrl+d

6. View installed dependencies

$ pipenv graph

7. Update the upgrade package

$ pipenv update requests

8. Install the virtual environment through requirements. txt

pipenv install -r requirements.txt

9. Generate the requirements. txt file

pipenv lock -r [--dev] > requirements.txt

10. Delete the virtual environment

pipevn --rm

Extended reading: https://rgb-24bit. github. io/blog/2018/pipenv.html


Related articles: