Published on: 25 Oct, 2021

Updated on: 25 Oct, 2021

How To Install Different Dependencies In Different Python Virtual Env

Here we will discuss regarding setting up Python environment by installing dependencies

As we have already explored how to set up our Python virtual environment. Now, its time to install the packages that need to be part of the virtual environment. Here, we will be exploring different ways to install packages in python virtual environment

So, let get dive into and explore how to install packages in a virtual environment.

Here, I would like to frame a hypothetical situation where we have to work on two different projects.

Let's name our first project as A (Data Science) and the second project as B (Web Development).Here, both A & B project is completely different and both needed different python packages to start with.

Considering project A involves data manipulation coming from different sources such as flat files & databases. On the other side, project B involves backend development for one of the websites. Here, the challenging part is both projects need different packages and we have already discussed in one of our previous posts that it always good to have a virtual environment in such cases.

So let's create a virtual environment first and install the necessary dependencies for both the projects - A & B

Creation of a virtual environment

Since we have already covered this part in one of our posts. I would highly recommend you to please refer the same. Here is the link down below:

Let's explore how to create & work with Python virtual environment

Let's consider we have named project A & B virtual environment as a-venv and b-venv respectively.

Installing dependencies for project A & B

As we already know, whenever we start with any project there will be multiple dependencies/packages that we need to install to get started. Similarly in our case, there are some basic & mandatory packages that need to be installed

1. For project A

numpy pandas jupyter

These are just mandatory packages that need to be installed but yes there will be more

For project B

djangopsycopg2-binarydjango-storagesPillow

These are just mandatory packages that need to be installed but yes there will be more

So let's go and start installing packages for project A & B

There are multiple ways to install packages in Python and the most commonly used is - pip install

1. For project A

Open terminal/CMD and activate the virtual environment a-venv and execute the below commands

pip install numpy pip install pandas pip install jupyter

2. For project B

Open terminal/CMD and activate the virtual environment b-venv and execute the below commands

pip install django pip install psycopg2-binary pip install django-storages pip install Pillow

But if you observe the way we are installing the packages doesn't seem efficient since if we have multiple packages then it will be a tedious job. So to overcome this, pip has another feature where we can specify all the packages in one statement/command as follows.

1. For project A - pip install numpy pandas jupyter

For project B - pip install django psycopg2-binary django-storages Pillow

But again it doesn't seem much useful when we have more packages since the command becomes very clumsy and whenever we need to deploy our applications or code in a different environment we need to install the dependent packages and typing every time the same command with multiple packages will be a tedious job and there will be a chance of missing some of the packages.

So to make our job easy pip supports installing packages from .txt> files which makes package installation on different servers and in a different environment very handy and smooth.

Installing dependencies for project A & B using requirement.txt

1. Create a simple text file with name requirement.txt and paste all the packages with respective versions in it as follows:

a. For project A(requirement.txt)

b. For project B(requirement.txt)

2. Open the terminal/CMD and activate the virtual environment (a-venv or b-venv)

3. Once you are in a virtual environment (a-venv or b-venv), navigate to requirement.txt file & execute below command:

pip install -r requirement.txt

Make sure you pick the right requirement.txt file for each of the project.

For more insights about pip install, please refer this wonderful article: https://pip.pypa.io/en/stable/reference/pip_install/#argument-handling

Share with your love's one!