WeChat official account : O & M development story , author : Mr. Jiang
In the traditional mature scheme , In order to avoid the confusion of package version management , Usually we rely on the current Python Version creates a virtual environment , And use in the virtual environment pip Package management . Now there is a useful tool called pipenv To help us more convenient management package .
pipenv It's a Python Package management tools . It is requests author Kenneth Reitz The works of , Provide Python Management between versions of , Various package management . When you get familiar with it and use it , You know it's a little similar to the front end npm and yarn The smell of .
It can automatically create and manage virtual environments for projects . When you use pipenv When , He will create in the project root directory Pipfile, Used to record the version information of the package . When you use pipenv To install the package , It will be generated in the project root directory Pipfile.lock File to lock the version and dependency information of the installation package , Avoid build errors .
It mainly solves the following problems :
There is no need to be based on current Python The interpreter version is created and used separately virtualenv and pip Tools .
Disordered maintenance requirements.txt file , Use Pipfile and Pipfile.lock Instead of
Basic concepts :
Execute under a new project root pipenv install, Then he will automatically create a virtual environment , And generate a Pipfile file
When install The command does not pass parameters to specify the installation package , If there is Pipfile, Will automatically install all Pipfile Medium [packages] In the package
# MacOS Next
pip install pipenv
# shell Command Completion
echo 'eval "$(pipenv --completion)"' >> ~/.zshrc
# Centos7 The next step is the same , Only need to ~/.zshrc Change to /etc/profile Or other environment variable files
advantage :
Automatically associate project related virtualenv, It can load quickly virtualenv .
Provided pipenv replace pip And bring a dependency list Pipfile, And dependency locking Pipfile.lock.
Pipfile In addition to dependency lists, it also supports fixed pypi source address , Fix python edition .
Pipfile And support dev Dependency list .pipenv install Your package will be forced to use Pipfile The source of .
Use pipenv graph The command can see the dependency tree .
Can switch directly python2 and python3.
shortcoming :
Windows The command line terminal does not start with venv The name of , You don't know whether you are in a virtual environment , Easy to misoperate
Permanently set Pipfile After the source in the file ,Pipfile What you see in the document is the official source , But in fact, the source you set has gone .
# Create project directory
mkdir project1
cd project1
# You can specify Python Version to create a virtual environment
pipenv --python 3.10.4
# Because the project is new , So it will automatically generate Pipfile and Pipfile.lock file
pipenv install
# Do not enter the virtual environment to execute commands , By default, there are only the following packages , Harmony virtualenv It's the same in Chinese time
$ pipenv run pip list
Package Version
---------- -------
pip 21.3.1
setuptools 59.6.0
wheel 0.37.1
# Activate the virtual environment
pipenv shell
# Installation package
pipenv install requests
# View the dependencies of the package
pipenv graph
# Upgrade a package
pipenv update requests
# Exit virtual environment
exit
# Delete virtual environment . Deleting the virtual environment does not delete the project directory , Just delete the directory of the virtual environment
pipenv --rm
If you now share your project with the team , Like people A Need your project , He downloaded the project locally , Then run the project and install the dependent package first , How to solve the dependency package ?
mkdir project2
cd project2
# Then copy Pipfile and Pipfile.lock File to this directory , Execute the following command
pipenv install
This installs all the dependent packages required by your project
First set your environment variables
vi ~/.zshrc
# python pipenv
export PATH="$PATH:/Users/allenjol/.local/" >> ~/.zshrc
source ~/.zshrc
open pycharm, Create a project ,New environment using choice Pipenv, After the creation, you enter Pipenv The environment of .
official account : O & M development story
github:https://github.com/orgs/sunsharing-note/dashboard
Love life , Love operation and maintenance
If you think the article is good , Please click on the upper right corner to choose send to friends or forward to friends circle . Your support and encouragement is my biggest motivation . Please pay attention to me if you like ~