Pipenv Notes
Why Pipenv
When maintaining many Python projects,
different projects might use different versions of the same Python libraries.
Not using a virtual environment and installing all Python modules directly on your machine will lead to version conflicts.
In the past, the mechanism of virtualenv + requirements.txt allowed different projects to use different versions of the same package,
and also enabled new developers or production environments to quickly install the packages required by the project.
However, updating packages was quite troublesome,
requiring manual re-exporting of a new requirements.txt.
Furthermore, when a project had different environment requirements (e.g., development environment and production environment),
two sets of package configurations, requirements-prod.txt and requirements-dev.txt, had to be maintained.
Without pyenv, it was also impossible to switch between different Python versions.
Later, the officially recommended pipenv from Python solved these problems,
making it convenient to achieve the following with just commands:
- Create independent Python versions and package virtual environments.
- Install and record package versions in automatically generated
PipfileandPipfile.lock, while checking package security through package hash values. - Record package usage environments (separating development and production environments).
- Read
.envfiles to set environment variables for the virtual environment. - Automatically switch Python versions in the system (or Python versions installed with
pyenv).
Operating Pipenv
Install Pipenv
pip3 install pipenvPipenv Commands
Create an independent virtual environment for a specific Python version:
Navigate to the project directory.
pipenv --python 3.8- Note that the specified Python version must be available on the system; otherwise, you need to install it using
pyenv.
- Note that the specified Python version must be available on the system; otherwise, you need to install it using
Install packages
pipenv install flaskInstall development packages
Packages with
--devwill be placed under[dev-packages]in the automatically generatedPipfile.pipenv install pytest --devUninstall packages
pipenv uninstall flaskExecute scripts in the created virtual environment
pipenv run python server.pyOther commands like
pytestcan also be executed.pipenv run pytestEnter the virtual environment
pipenv shellTo exit the virtual environment, simply type
exit.Create a virtual environment using existing
PipfileandPipfile.lockpipenv installTo install development environment packages as well:
pipenv install --devCreate
PipfileandPipfile.lockfromrequirements.txtpipenv installOutput
requirements.txtThis is generally not necessary, but in some special cases (e.g., requirements for specific platforms), it can still be done.
pipenv lock --requirements > requirements.txtUpgrade packages in the virtual environment
pipenv updateDelete the current virtual environment
pipenv --rm
Upgrade Pipenv
pip3 install --upgrade pipenv