Pipenv Tutorial
This tutorial will walk you through how to install pipenv
and manage packages and dependencies with pipenv
.
Detailed pipenv doc ref to : https://realpython.com/pipenv-guide/
Prerequisite
- Install python interpreter.
- Install pip
- Upgrade pip to latest version
Upgrade pip:
1 | python -m pip install --upgrade pip |
Use system wide pip
to install pipenv
:
1 | pip install pipenv |
Check pipenv
installation location:
1 | where pipenv |
Output:
1 | C:\python37\Scripts\pipenv.exe |
Check pipenv version:
1 | pipenv --version |
Output
1 | pipenv, version 2018.7.1 |
Check system wide python packages:
1 | pip list |
Output:
1 2 3 4 5 6 7 8 | Package Version ---------------- --------- certifi 2018.4.16 pip 18.0 pipenv 2018.7.1 setuptools 40.0.0 virtualenv 16.0.0 virtualenv-clone 0.3.0 |
Create virtual environment with pipenv
Create a project folder and cd to that folder and run pipenv install
to initialize an empty virtual environment:
1 2 | cd c:\demo\project-a
pipenv install
|
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | Creating a virtualenv for this project... Pipfile: C:\demo\project-a\Pipfile Using c:\python37\python.exe (3.7.0) to create virtualenv... Already using interpreter c:\python37\python.exe Using base prefix 'c:\\python37' Virtualenv location: C:\Users\user_name\.virtualenvs\project-a-x_uLKVn6 Creating a Pipfile for this project... Pipfile.lock not found, creating... Locking [dev-packages] dependencies... Locking [packages] dependencies... Updated Pipfile.lock (a65489)! Installing dependencies from Pipfile.lock (a65489)... ================================ 0/0 - 00:00:00 To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. |
Pipfile
and Pipfile.lock
will be created in this project folder. Virtual environment will be created in default path C:\Users\user_name\.virtualenvs\project-a-x_uLKVn6
.
Create a portable venv
Open a cmd and set environment variable WORKON_HOME
:
1 | export WORKON_HOME=~/venv |
Run pipenv install
to create a virtual environment in desired directory other than user's directory. Copy and paste the whole venv folder to the same directory on other machine. To active this environment by running the active shell program in ./Scripts
folder for different platform.
Install packages with pipenv
Change directory to project folder and install packages with pipenv install
instead of pip install
:
1 2 | pipenv install psutil==5.4.8 pipenv install requests |
A specific version number will be recorded in Pipfile
or a *
will be recorded if version number is not provided. Example content of Pipfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 | [[source]] name = "pypi" url = "https://pypi.org/simple" verify_ssl = true [dev-packages] [packages] psutil = "==5.4.8" requests = "*" [requires] python_version = "3.7" |
The Pipfile.lock
will be generated by pipenv and all the packages and dependencies with specific versions at this time will be recorded. You can produce the exact same environment with Pipfile.lock
by running command pipenv sync
to other project folder or other machine with the same platform.
Note
DO NOT change Pipfile.lock
manually since it contains the hashes of all the dependencies.
Activate pipenv environment
Spawn a sub shell to activate this environment:
1 | pipenv shell |
Output:
1 | (demo-liOaUuB5) C:\pipenv\demo |
Hit exit
to exit pipenv shell.
Active virtual environment without entering sub shell:
1 2 3 | pipenv run pip list
# or
pipenv run python hello.py
|
Reproduce environment from Pipfile.lock
Copy Pipfile
and Pipfile.lock
to your project folder and restore the same environment:
1 | pipenv sync |
The sync
sub command will create and restore the environment from Pipfile.lock
.
Set Pycharm interpreter with existing Pipfile
Pycharm creates pipenv virtual environment from Pipfile.lock
by default. Copy Pipfile
and Pipfile.lock
to project root folder. Open pycharm settings window(Ctrl+Alt+S). Add a new Python interpreter by selecting pipenv environment. Wait until it's finished then check the installed packages specified in Pipfile.
Tip
Pycharm could fail to create pipenv from Pipfile sometimes, you might consider create virtual environment from commandline manually.