Pipenv & Virtual Environments — The Hitchhiker's Guide to Python (2024)

Pipenv & Virtual Environments — The Hitchhiker's Guide to Python (1)

This tutorial walks you through installing and using Python packages.

It will show you how to install and use the necessary tools and make strongrecommendations on best practices. Keep in mind that Python is used for a greatmany different purposes, and precisely how you want to manage your dependenciesmay change based on how you decide to publish your software. The guidancepresented here is most directly applicable to the development and deployment ofnetwork services (including web applications), but is also very well suited tomanaging development and testing environments for any kind of project.

Note

This guide is written for Python 3, however, these instructionsshould work fine on Python 2.7—if you are still using it, for some reason.

Make sure you’ve got Python & pip

Before you go any further, make sure you have Python and that it’s availablefrom your command line. You can check this by simply running:

$ python --version

You should get some output like 3.6.2. If you do not have Python, pleaseinstall the latest 3.x version from python.org or refer to theInstalling Python section of this guide.

Note

If you’re newcomer and you get an error like this:

>>> pythonTraceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'python' is not defined

It’s because this command is intended to be run in a shell (also calleda terminal or console). See the Python for Beginnersgetting started tutorial for an introduction to using your operatingsystem’s shell and interacting with Python.

Additionally, you’ll need to make sure you have pip available. You cancheck this by running:

$ pip --version

If you installed Python from source, with an installer from python.org, orvia Homebrew you should already have pip. If you’re on Linux and installedusing your OS package manager, you may have to install pip separately.

Installing Pipenv

Pipenv is a dependency manager for Python projects. If you’re familiarwith Node.js’ npm or Ruby’s bundler, it is similar in spirit to thosetools. While pip can install Python packages, Pipenv is recommended asit’s a higher-level tool that simplifies dependency management for common usecases.

Use pip to install Pipenv:

$ pip install --user pipenv

Note

This does a user installation to prevent breaking any system-widepackages. If pipenv isn’t available in your shell after installation,you’ll need to add the user base’s binary directory to your PATH.

On Linux and macOS you can find the user base binary directory by runningpython -m site --user-base and adding bin to the end. For example,this will typically print ~/.local (with ~ expanded to theabsolute path to your home directory) so you’ll need to add~/.local/bin to your PATH. You can set your PATH permanently bymodifying ~/.profile.

On Windows you can find the user base binary directory by runningpy -m site --user-site and replacing site-packages withScripts. For example, this could returnC:\Users\Username\AppData\Roaming\Python36\site-packages so you wouldneed to set your PATH to includeC:\Users\Username\AppData\Roaming\Python36\Scripts. You can set youruser PATH permanently in the Control Panel. You may need to logout for the PATH changes to take effect.

Installing packages for your project

Pipenv manages dependencies on a per-project basis. To install packages,change into your project’s directory (or just an empty directory for thistutorial) and run:

$ cd project_folder$ pipenv install requests

Pipenv will install the excellent Requests library and create a Pipfilefor you in your project’s directory. The Pipfile is used to track whichdependencies your project needs in case you need to re-install them, such aswhen you share your project with others. You should get output similar to this(although the exact paths shown will vary):

Creating a Pipfile for this project...Creating a virtualenv for this project...Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6'New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/pythonInstalling setuptools, pip, wheel...done.Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBdInstalling requests...Collecting requests Using cached requests-2.18.4-py2.py3-none-any.whlCollecting idna<2.7,>=2.5 (from requests) Using cached idna-2.6-py2.py3-none-any.whlCollecting urllib3<1.23,>=1.21.1 (from requests) Using cached urllib3-1.22-py2.py3-none-any.whlCollecting chardet<3.1.0,>=3.0.2 (from requests) Using cached chardet-3.0.4-py2.py3-none-any.whlCollecting certifi>=2017.4.17 (from requests) Using cached certifi-2017.7.27.1-py2.py3-none-any.whlInstalling collected packages: idna, urllib3, chardet, certifi, requestsSuccessfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22Adding requests to Pipfile's [packages]...P.S. You have excellent taste! ✨ 🍰 ✨

Using installed packages

Now that Requests is installed you can create a simple main.py file touse it:

import requestsresponse = requests.get('https://httpbin.org/ip')print('Your IP is {0}'.format(response.json()['origin']))

Then you can run this script using pipenv run:

$ pipenv run python main.py

You should get output similar to this:

Your IP is 8.8.8.8

Using $ pipenv run ensures that your installed packages are available toyour script. It’s also possible to spawn a new shell that ensures all commandshave access to your installed packages with $ pipenv shell.

Next steps

Congratulations, you now know how to install and use Python packages! ✨ 🍰 ✨

virtualenv is a tool to createisolated Python environments. virtualenv creates a folder which contains all thenecessary executables to use the packages that a Python project would need.

It can be used standalone, in place of Pipenv.

Install virtualenv via pip:

$ pip install virtualenv

Test your installation:

$ virtualenv --version

Basic Usage

  1. Create a virtual environment for a project:
$ cd project_folder$ virtualenv venv

virtualenv venv will create a folder in the current directory which willcontain the Python executable files, and a copy of the pip library which youcan use to install other packages. The name of the virtual environment (in thiscase, it was venv) can be anything; omitting the name will place the filesin the current directory instead.

Note

‘venv’ is the general convention used globally. As it is readily available in ignore files (eg: .gitignore’)

This creates a copy of Python in whichever directory you ran the command in,placing it in a folder named venv.

You can also use the Python interpreter of your choice (likepython2.7).

$ virtualenv -p /usr/bin/python2.7 venv

or change the interpreter globally with an env variable in ~/.bashrc:

$ export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python2.7
  1. To begin using the virtual environment, it needs to be activated:
$ source venv/bin/activate

The name of the current virtual environment will now appear on the left ofthe prompt (e.g. (venv)Your-Computer:project_folder UserName$) to let you knowthat it’s active. From now on, any package that you install using pip will beplaced in the venv folder, isolated from the global Python installation.

For Windows, the same command mentioned in step 1 can be used to create a virtual environment. However, activating the environment requires a slightly different command.

Assuming that you are in your project directory:

C:\Users\SomeUser\project_folder> venv\Scripts\activate

Install packages using the pip command:

$ pip install requests
  1. If you are done working in the virtual environment for the moment, you candeactivate it:
$ deactivate

This puts you back to the system’s default Python interpreter with all itsinstalled libraries.

To delete a virtual environment, just delete its folder. (In this case,it would be rm -rf venv.)

After a while, though, you might end up with a lot of virtual environmentslittered across your system, and it’s possible you’ll forget their names orwhere they were placed.

Note

Python has included venv module from version 3.3. For more details: venv.

Other Notes

Running virtualenv with the option --no-site-packages will notinclude the packages that are installed globally. This can be usefulfor keeping the package list clean in case it needs to be accessed later.[This is the default behavior for virtualenv 1.7 and later.]

In order to keep your environment consistent, it’s a good idea to “freeze”the current state of the environment packages. To do this, run:

$ pip freeze > requirements.txt

This will create a requirements.txt file, which contains a simplelist of all the packages in the current environment, and their respectiveversions. You can see the list of installed packages without the requirementsformat using pip list. Later it will be easier for a different developer(or you, if you need to re-create the environment) to install the same packagesusing the same versions:

$ pip install -r requirements.txt

This can help ensure consistency across installations, across deployments,and across developers.

Lastly, remember to exclude the virtual environment folder from sourcecontrol by adding it to the ignore list (see Version Control Ignores).

virtualenvwrapper

virtualenvwrapperprovides a set of commands which makes working with virtual environments muchmore pleasant. It also places all your virtual environments in one place.

To install (make sure virtualenv is already installed):

$ pip install virtualenvwrapper$ export WORKON_HOME=~/Envs$ source /usr/local/bin/virtualenvwrapper.sh

(Full virtualenvwrapper install instructions.)

For Windows, you can use the virtualenvwrapper-win.

To install (make sure virtualenv is already installed):

$ pip install virtualenvwrapper-win

In Windows, the default path for WORKON_HOME is %USERPROFILE%\Envs

Basic Usage

  1. Create a virtual environment:
$ mkvirtualenv project_folder

This creates the project_folder folder inside ~/Envs.

  1. Work on a virtual environment:
$ workon project_folder

Alternatively, you can make a project, which creates the virtual environment,and also a project directory inside $WORKON_HOME, which is cd-ed intowhen you workon project_folder.

$ mkproject project_folder

virtualenvwrapper provides tab-completion on environment names. It reallyhelps when you have a lot of environments and have trouble remembering theirnames.

workon also deactivates whatever environment you are currently in, so youcan quickly switch between environments.

  1. Deactivating is still the same:
$ deactivate
  1. To delete:
$ rmvirtualenv venv

Other useful commands

lsvirtualenv
List all of the environments.
cdvirtualenv
Navigate into the directory of the currently activated virtual environment,so you can browse its site-packages, for example.
cdsitepackages
Like the above, but directly into site-packages directory.
lssitepackages
Shows contents of site-packages directory.

Full list of virtualenvwrapper commands.

virtualenv-burrito

With virtualenv-burrito, youcan have a working virtualenv + virtualenvwrapper environment in a single command.

direnv

When you cd into a directory containing a .env, direnvautomagically activates the environment.

Install it on Mac OS X using brew:

$ brew install direnv

On Linux follow the instructions at direnv.net

Pipenv & Virtual Environments — The Hitchhiker's Guide to Python (2024)

FAQs

Should I use virtualenv or Pipenv? ›

If you are working with your personal projects and not installing pipenv, I recommend installing pyenv-virtualenv. If you are working in a team or with more than one system, I recommend you to install pipenv which I am covering next.

What is Pipenv in Python? ›

Pipenv is a tool that provides all necessary means to create a virtual environment for your Python project. It automatically manages project packages through the Pipfile file as you install or uninstall packages. Pipenv also generates the Pipfile.

What is the difference between pip and Pipenv? ›

While pip can install Python packages, Pipenv is recommended as it's a higher-level tool that simplifies dependency management for common use cases. This does a user installation to prevent breaking any system-wide packages.

Which Python does Pipenv use? ›

pipenv uses a different pyenv installed python to create the venv: artlogic@wardenclyffe:~/Code/random/pipenv-again$ pipenv install -v requests Using python: None Path to python: /home/artlogic/. pyenv/root/versions/3.8.

Which virtual environment is best for Python? ›

Virtualenv is the most common and easy to install tool for virtual environments. It's a great tool for beginners. Easy to use in the deployed environments. The most common tool for python virtual environments, so it has lots of documentation for many issues.

Should I use conda or Pipenv? ›

If you need to create multiple projects for websites using Python, then isolate these projects with pipenv instead of conda. For those projects managed by the Anaconda Project, if you need to install a pure Python library, use the pip package first.

Why should I use Pipenv? ›

This virtualenv requires pip as a dependency management system. pip can install Python packages, Pipenv is recommended as it's a higher-level tool that simplifies dependency management for common use cases. This is the major reason why we use pipenv over pip.

Why do I need Pipenv? ›

Pipenv is a packaging tool for Python that solves some common problems associated with the typical workflow using pip , virtualenv , and the good old requirements. txt . In addition to addressing some common issues, it consolidates and simplifies the development process to a single command line tool.

How do you use Pipenv in Python? ›

Set up a new project with Pipenv

Open a console in your project directory and type pipenv install <package_name> to install a package for the project. To specify that the package is for development, use the -d flag. You can use pip syntax to denote a specific version of a package (e.g., black==13.0b1 ).

Is virtualenv deprecated? ›

The ActiveState Platform is a modern solution to dependency management. Virtualenv has been deprecated in Python 3.8.

Should I use Virtualenvwrapper? ›

Do use virtualenvwrapper to manage Python virtual environments. One way to make virtual environments less burdensome is to use virtualenvwrapper . This tool allows you to manage all of the virtual environments in your workspace from a single, central command-line app.

What is the difference between virtualenv and Virtualenvwrapper? ›

Virtualenvwrapper is a utility on top of virtualenv that adds a bunch of utilities that allow the environment folders to be created at a single place, instead of spreading around everywhere.

What is the purpose of virtualenv? ›

virtualenv is used to manage Python packages for different projects. Using virtualenv allows you to avoid installing Python packages globally which could break system tools or other projects. You can install virtualenv using pip.

Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6660

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.