DEV Community

Cover image for How to Setup Virtual Environment Using Pipenv

Posted on

How to Setup Virtual Environment Using Pipenv

Virtual Environments using PipEnv

PipEnv is a new way to create Virtual Environments in Python that allows for
Environment Agnostic dependency installation.

It makes use of;

  1. Pip(Python package manager)

  2. Virtualenv(used for creating isolated Python environments to easily use different packages in different projects)

  3. Pipfile(records the package versions used in a given project so you can
    easily install them on any other system you want. Also considered as
    the replacement of Python requirements files)

Installing PipEnv

  pip install --user pipenv

Next, we are going to set up the requirements.txt file to make sure you have
all the necessary dependencies for the rest of the book.

Versions – Requirements.txt File

The versions that you will need to follow this tutorial is provided for you by
the following requirements.txt file.

The instructions that you need to follow are also provided and should work on any Operating System. Here is the requirements.txt file that we are going to use:


Copy and Paste the above in a file labeled requirements.txt in the root directory of your app. Make sure you are inside of that directory.Then, run the following command:

       pipenv install -r requirements.txt

Using Pip inside your virtual environment

An alternative to the requirements.txt file above is to use pip inside your
Virtual Environment.

      pipenv shell
      pipenv install django
      pipenv install djangorestframework

Once it’s finished, you should have a PipFile file and a PipFile.lock file.

Congratulations! You installed PipEnv and you have an environment that is ready to go for the development for the rest of the book.

Everytime you sit at the computer you should do this…

When you’re ready to sit down and work on your project, you should do the

Open up your terminal and go to the directory with the PipFile file and a
PipFile.lock file. Then, type:

      pipenv shell

Your shell should be changed – it should be noticeably different. Mine
looks like this:

     (token_auth_app) bash-3.2$

Yours should be similar, although what is inside of the parentheses might be

Everytime you are done working on your app you should do this…

When you’re done working, you should deactivate the Virtual Envionment by
running the following two commands:


This will kick you out of the Virtual Environment so you don’t mess up the virtual environment for later.

Other commands related to Pipenv shell include:

Checks for security vulnerabilities;

       pipenv check

Will run a command from the virtualenv;

       pipenv run *

Shows you a graph of your installed dependencies;

       pipenv graph

Check local packages

      pipenv lock -r

Install a dev package

      pipenv install nose --dev

Ignore pipfile

     pipenv install --ignore-pipfile

Set lockfile - before deployment

     pipenv lock

Synchronize to use an existing pipenv

     pipenv sync

So basically with one short pipenv command you can install a package in a virtual environment and record it in pipfile and pipefile.lock files. Think of pipenv as next gen package management tool that should save you time and also provide you with new shiny utilities.

Thanks for the read.

Top comments (0)