Using Pipenv to Manage Python Packages and Versions
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
What is Pipenv?
Pipenv is Python’s officially recommended package management tool. It combines the functionality of Pip and Virtualenv, along with the best features of packaging tools from other languages such as Bundler and NPM. This results in a simplified workflow for installing packages and managing virtual environments.
You will need to have Python installed on your system before installing Pipenv.
If you are using Ubuntu 17.10 or higher, you can install Pipenv directly from the Pypa ppa:
sudo apt install software-properties-common python-software-properties sudo add-apt-repository ppa:pypa/ppa sudo apt update sudo apt install pipenv
Other distributions should first install Pip and use it to install Pipenv:
sudo apt install python-pip pip install pipenv
Create a directory for an example Python project:
mkdir python-example && cd python-example
Create a virtual environment in the directory:
pipenv --python 3.6
Creating a virtualenv for this project… Using /home/username/miniconda3/bin/python3.6m (3.6.4) to create virtualenv… ⠋Running virtualenv with interpreter /home/username/miniconda3/bin/python3.6m Using base prefix '/home/username/miniconda3' New python executable in /home/username/.local/share/virtualenvs/python-example-YJNpmGYi/bin/python3.6m Also creating executable in /home/username/.local/share/virtualenvs/python-example-YJNpmGYi/bin/python Installing setuptools, pip, wheel...done. Virtualenv location: /home/username/.local/share/virtualenvs/python-example-YJNpmGYi Creating a Pipfile for this project…
If you omit the
--pythonoption, the environment will be created with your system’s default version of Python.
Check the contents of the directory with
ls; you will see that a
Pipfilehas been created automatically. View this file in a text editor:
- File: ~/python-example/Pipfile
1 2 3 4 5 6 7 8 9 10 11
[[source]] url = "https://pypi.python.org/simple" verify_ssl = true name = "pypi" [dev-packages] [packages] [requires] python_version = "3.6"
Install Numpy. Pipenv will automatically add the dependency to the
[packages]section in the Pipfile. In addition, Pipenv creates a file named
Pipfile.lock, which contains a hash of the exact versions used. This ensures that when other developers install the dependencies for this project, they will all end up with exactly the same versions.
pipenv install numpy
Install a specific version of Pytest as a development dependency:
pipenv install --dev 'pytest>=3.*'
View the changes these installations have made to the Pipfile:
- File: Pipfile
1 2 3 4 5 6 7 8 9 10 11 12 13
[[source]] url = "https://pypi.python.org/simple" verify_ssl = true name = "pypi" [dev-packages] pytest = ">=3.*" [packages] numpy = "*" [requires] python_version = "3.6"
Since no version was specified during when installing Numpy, the Pipfile specifies that any version (
"*") is acceptable. The specific version installed is recorded in
If you install a package in a directory that does not have a Pipfile, Pipenv will create a new environment in that directory automatically, using your system’s default Python version. This means that the commands in this section can be condensed into two steps:
pipenv install numpy pipenv install --dev pytest
Work with Virtual Environments
From within the directory containing the Pipfile, start a shell in the new environment:
This is similar to running
Launch the Python interpreter from inside this shell:
You should be able to import any of the installed packages:
>>> import pytest >>> import numpy as np
Exit the shell (similar to deactivating an environment with
View your project’s dependencies in graph form:
numpy==1.14.2 pytest==3.5.0 - attrs [required: >=17.4.0, installed: 17.4.0] - more-itertools [required: >=4.0.0, installed: 4.1.0] - six [required: >=1.0.0,<2.0.0, installed: 1.11.0] - pluggy [required: >=0.5,<0.7, installed: 0.6.0] - py [required: >=1.5.0, installed: 1.5.3] - setuptools [required: Any, installed: 39.0.1] - six [required: >=1.10.0, installed: 1.11.0]
The graph includes the packages you installed as well as their dependencies.
Locate the binary for the virtual environment:
For a full list of commands and options, see the Pipenv GitHub repo and official docs.
This page was originally published on