Pipenv
Pipenv
Pipenv
is a tool that can create virtual environments and manage dependencies. An alternative to this tool is Poetry
.
It uses the
pip
package manager to install packages and has an advanced dependency resolver that checks for compatibility issues.
You can combine
pyenv
withpipenv
to manage the Python versions and the virtual environments.
Creating a new project
This will create a new project in the current directory. It will create a new virtual environment and store the dependencies in the Pipfile
file and the versions in the Pipfile.lock
file.
Pipfile.lock
file will also contains the hashes of the packages to ensure that the package is not tampered with.
pipenv install <package_name>
Installing from Pipfile
file
Will install the dependencies listed in the Pipfile
file.
pipenv install
Updating dependencies
Will update the dependencies to the latest version and update the Pipfile.lock
file. There won’t be any dependency conflicts.
pipenv update
Running Environment commands
# Running a command in the virtual environment
pipenv run <command>
# Running an interactive shell
pipenv shell
# Updating the dependencies
pipenv update
Deploying to production
When deploying to production, it is recommended to use the --deploy
flag to ensure that the dependencies are locked to the versions in the Pipfile.lock
file.
For non-interative shells, you can use the environment by
cd <project directory> && /usr/local/bin/pipenv run python script.py
.
mkdir ../pipenv_production
cp Pipfile Pipfile.lock ../pipenv_production
pipenv install --deploy