Python virtual environment using pipenv

Yashod Perera
Analytics Vidhya
Published in
3 min readMar 21, 2020

What is a virtual environment?

The common problem where most of the developer’s face is dependency issue where the developers develop several projects which needed different versions of dependencies.

As the following example let’s take that the Flask 1.0 and Django2.4 is globally install on the machine and Following projects are needed different version of flask and Django. So how can we manage versions across the projects. That is where the virtual environments come to place.

The solution is to make separate virtual environments across the different projects and install the required dependencies and versions on those particular virtual environments. Then those installed dependencies do not mix with the global dependencies and versions as follows.

What is pip and pipenv?

pip is the package manager for python and it simply install the packages with the required versions for python. Pip is included default after python 3.5 you can simply check whether the pip is installed entering

pip –version

How to install pip?

Step 1 : Download the source file

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Step 2 : Then run the file using python

python get-pip.py

Note : There are specific ways to install in linux and mac but the above method is generic way for all operating systems.

Let’s dive into how to make virtual environments and work with them.

1. Create a virtual environment

a. Make the project directory

mkdir project && cd project

b. Make a directory for manage the virtual environment files

mkdir environment && cd environment

c. Initiate the virtual environment

For python 3

pipenv –-three

For python 2

pipenv –-two

d. Start the virtual environment

pipenv shell

Note :- If there is no Pipfile file then new file is created and the virtual environment is up and running. Pipfile include all the requires and packages and if you install any packages then Pipfile.lock is created which includes dependencies.

You can simply install any python package using

pipenv install <package_name> 

For specific version

pipenv install “package_name=version_number”

2. Install dependencies using external text file

This is important when we work on a project and we have to install the dependencies which others using. For that pip env facilitate the way to generate the dependencies as follows using a text file.

Text file format

First you have to start the virtual environment using pipenv shell for that you must go to the directory which hold the pipfile. Then simply type as follows,

pipenv install -r <txt_file.txt>

Then all the dependencies with respective versions will be installed on the virtual environment.

3. View installed dependencies

You can view the installed dependencies using two commands.

pipenv graph
pipenv graph command output
pipenv run pip freeze
pipenv run pip freeze command output

Note :- pip freeze will show all the dependencies on the machine and to view the dependencies inside the virtual environment we run that command inside the environment using pipenv run command.

4. Uninstall installed package in virtual environment

This can be simply done using following command

pipenv uninstall <package_name>

5. Exit from the virtual environment

This can be done using exit command.

References : https://pipenv-fork.readthedocs.io/en/latest/

--

--

Yashod Perera
Analytics Vidhya

Technical Writer | Tech Enthusiast | Open source contributor