Setting up a python project with virtualenv & creating the requirements.txt file

Setting up a python project with virtualenv & creating the requirements.txt file

Installing packages globally is not a good idea. Isolating required packages and their specific versions, will help you deploy, share projects with your team mates a lot easier. We'll be using virtualenv in this tutorial.

Say we want to use the pandas package to do some dataframe operations.

For eg : I have a python dict, that I want to operate, so we'll go ahead and create a dataframe.

import pandas as pd 

data = {"names": ["dan", "rob", "ned"], "age": [25, 30, 60]}
df = pd.DataFrame(data)
print(df)

All the code does is, create a dataframe from a python dict and print it to the console.

Since we're using pandas as our dependency, we'll include that as our dependency using virtualenv.

To create a virtualenv.

virtualenv env

To activate the virtualenv :

source env/bin/activate

To include pandas package :

pip install pandas

To view installed packages ;

pip freeze

To save installed packages in virtualenv to a file :

pip freeze > requirements.txt