Byte Sized Hacks: Manage dependencies of your Python Project with pipenv

ওয়াসী (Wasi)
Dreamcatcher IT’s Blog
4 min readJan 12, 2018

--

For last couple of years or so, I extensively used virtualenv for maintaining a sand boxed environment for my Python projects with all the dependencies. I followed the typical pattern consisting of creating requirements.txt and updating it time to time by pip freeze > requirements.txt.

It enables you to experiment with different python modules without making changes to system. For any projects, all you have to do is create an environment first! using virtualenv command (Of course, you will have to install it first, if you don’t have it already).

virtualenv -p <your_python_path> <virtual_environment_name>

e.g

virtualenv -p /usr/bin/python3.6 .env

After creating the environment you will have to activate it i.e. assuming you are in the project directory, you can activate the virtual environment with:

source .env/bin/activate

Note: .env is the environment_name

This way, you can easily create a separate environment for each of your projects. However, there are some issues that are pretty cumbersome. For example, in some cases, you might need to install additional modules for aiding you in the development for testing/debugging. To maintain such dependencies one solution is to create multiple requirements.txt files like, requirements_dev.txt etc. for keeping development environment with additional packages for debugging separate from the production environment.
Managing these things are really tough. By creating aliases for commands you can ease up it a little bit but, its still the same. And, every time you start on a project you will have to create environment also, activate the environment each time you work itself is pretty bothering…

While I was constantly looking for alternatives I found pipenv along with others. I liked the simplicity of pipenv! And, started to explore it.

Guess what, it is now officially recommended Python packaging tool from python.org

Meet pipenv!

Kenneth’s pipenv is extremely helpful for managing projects with less headache! It combines Pip, Pipfile and Virtualenv and provides a command line tool which is pretty straightforward. I just love it.

Installation

Cakewalk! Just use pip:

pip install pipenv

After installation you can literally forget pip! pipenv will take care of all the dependencies along with the virtual environment. It generates Pipfile Pipfile.lock which is similar to requirements.txt keeps all your dependencies noted. To start a new project all you have to do is create a new project folder. open it in terminal. And, run the following command:

pipenv install

This will look for existing environments if there is none, it will create a new one for you and initialize it… Thats it! No need to execute virtualenv commands or anything! You can use--two or --three arguments along with the above command e.g. pipenv install --three for creating python 3 environment. If you don’t specify it will use the system preferred version (output of which python )

Managing Development Environments with pipenv

Coolest thing of pipenv is, you can install modules in development mode by passing --dev as argument! It will install it only for development environment. For example

pipenv install --dev django_debug_toolbar

will install the django_debug_toolbar module only for dev! So, if you run

pipenv install

in your production it won’t install django_debug_toolbar! but if you run

pipenv install --dev

it will only then install all the development packages along with the production packages. Simple right?

So, all you have to do is to share the pipfile with your teammates (you can include this file on git repo as well) then they can replicate your dev/prod environment without a single hassle!

So, how to activate the virtualenv within pipenv?

As, I have mentioned before you don’t have to activate or deactivate your virtualenv if you already have installed pipenv
To invoke a python shell in your virtual environment you can simply use the following command:

pipenv shell

It will open up the python Interactive shell using the project environment! To run a python file you can similarly use:

pipenv run python your_script.py

This will run your_script.py file using the virtual environment.

Okay, that is it! You can explore the pipenv documentation to know more about it. Or, if you like you can checkout the source from their github repository

Author:

Wasi Mohammed Abdullah

Thinker, Day Dreamer, Python Enthusiast, Javascript Admirer An Introvert with Exception!

CEO, Founder
Dreamcatcher IT
twitter: twitter.com/wasi0013
github:
github.com/wasi0013
facebook: fb.me/wasi0013

P.S. I have switched from medium to my personal blog. Visit my blog to get latest posts! You can also suggest topic of your choice by contacting me, I will try my best to write about it. :)

Personal Blog: https://wasi0013.com/blog

--

--