Python : How to setup your Virtual environment correctly using Pipenv


Pipenv is a dependency manager for Python projects. If you’re familiar with Node.js’ npm or Ruby’s bundler, it is similar in spirit to those tools.

Everytime you want to create a new Python project or you follow a new course, you get sucked with the VirtualEnv ? What the heck virtualenv is ? How you can setup it Correctly ?


Python : 3.4 or later pip

Package Manager VS Dependency Manager

Based on a StackOverFlow answer

Package Manager - is used to configure system, ie to setup your development environment and with these settings you can build many projects.

Dependency Manager - Is specific to project. You manage all dependencies for a single project and those dependencies are going to be saved on your project. When you start another project you should manage your dependencies again.

Why we need it

The main purpose of using a dependency manager is to separate your application dependencies, that will give you the ability to use the one Framework in different project with different version.

A simple use case :

Imagine we have two(2) Django applications and we want to install differents versions of Django.

What we do ?

We've the choice to install all versions in one machine, but this is not the ideal way to do.

How to setup your virtualenv Correctly


As Pythonist we've many choices

  • Poetry
  • Pip
  • Pipenv
  • etc...

In this tutorial we will use pipenv and i think is the easiest to setup. And it is a recommended for collaborative and Team projects.

Make sure you've Python and Pip installed in your machine

Let's check your installation

$ python --version

The output should look something like this

Python 3.7.5

And check the pip installation

$ pip --version

The output

pip 19.3.1 from /usr/local/lib/python3.7/dist-packages/pip (python 3.7

Installing pipenv using pip

$ pip install --user pipenv

Check the installation

$ pipenv --version

The output

pipenv, version 11.9.0

Create new project

$ mkdir test_pipen && cd test_pipenv
$ touch

Installing packages for your project

$ pipenv install requests
Creating a virtualenv for this project…
Using /usr/bin/python3 (3.7.5) to create virtualenv…
⠋Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/username/.local/share/virtualenvs/test_pipenv-3gXMtvzy/bin/python3
Also creating executable in /home/username/.local/share/virtualenvs/test_pipenv-3gXMtvzy/bin/python
Installing setuptools, pip, wheel...

Virtualenv location: /home/username/.local/share/virtualenvs/test_pipenv-3gXMtvzy
Creating a Pipfile for this project…
Installing requests…
Looking in indexes:
Collecting requests
 Using cached
Collecting idna<2.9,>=2.5
 Using cached
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
 Using cached
Collecting chardet<3.1.0,>=3.0.2
 Using cached
Collecting certifi>=2017.4.17
 Using cached
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.7

Adding requests to Pipfile's [packages]…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (5a8f8c)!
Installing dependencies from Pipfile.lock (5a8f8c)…
 🐍   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/5 — 00:00:01

Activate your new environment

$ pipenv shell
$ (test_pipenv-3gXMtvzy) ....

Add this line in your file

import requests

response = requests.get('')

print('Your IP is {0}'.format(response.json()['origin']))

Run the application

$ python

You should get output similar to this:

Your IP is,

See you next :-)

© 2021 Ousseynou • Crafted with ❤️ Let's talk