pip install multiple packages in Python

pip-install-multiple-packages

If you are working or learning Python then you must use different packages or libraries to develop something new. For some projects sometimes you may need to install multiple packages at once. In this post, we will explore different ways to install multiple packages in Python using pip or without pip.

Method 1: Install multiple packages using pip

The simplest way to install multiple packages in Python is by using the pip install command. To do this, you just need to mention list of packages you want to install separated by a space after the pip install command.

For example, if you want to install numpy and pandas, you can run the following command in your terminal:

pip install numpy pandas

Above single-line command will install multiple packages (numpy and pandas) at once. Note that, the above command will install packages in a specific order. This means that pip will first install numpy package then it will install pandas.

Install specific versions of different packages

Now if you want to install a specific version of any package you want to install. Using the below command you can do that.

pip install numpy==1.19.3 pandas==1.2.0

Above command will install numpy version 1.19.3 and pandas version 1.2.0 in your Python environment.

Method 2: Install packages from a requirements file

Another common way to install multiple packages using requirements files. While implementing any GitHub project you may see that the GitHub projects contain a riqurement.txt file.

A requirements file is nothing but a simple text file which contains list of all the packages you want to install along with their version numbers.

To create a requirements file, simply open a text editor and list the packages you want to install, each on a new line, in the format package_name==version_number. For example, if you want to install spacy libray with version 3.5.1 and NLTK version 3.8.1, you need to mention those in the text file like below:

spacy==3.5.1
nltk==3.8.1

Once you mention those in that text file, save this file as requirements.txt in your project directory or working directory.

Also Read:  Install MiniConda & setup Environment for Machine Learning

You can install those multiple packages listed in the requirements file using below pip command. This will install all the packages listed in the file along with their specified versions.

pip install -r requirements.txt

Install multiple packages from different requirements files

Now let’s say you have multiple requirements files for different projects or virtual environments. In that case, you can use below command to install all packages from all the requirements.txt files.

pip install -r requirements1.txt -r requirements2.txt

Method 3: Install packages using a loop

If you have a long list of packages to install, you can also use a loop inside Python code to automate the process. For example, you can create a Python script that reads a list of packages from a file and installs them one by one. This is an alternate option of requirement.txt.

import subprocess

with open('packages.txt', 'r') as f:
    packages = f.read().splitlines()

for package in packages:
    subprocess.call(['pip', 'install', package])

This script reads the package names from a file named packages.txt. Then loops through the list and runs the pip install command for each package.

Method 4: Install multiple modules of the same package

Now if you want to install multiple modules of the same package, you can mention those in your pip command like below.

pip install matplotlib-base matplotlib-pyplot

Above command will install two modules (matplotlib-base and matplotlib-pyplot) of the matplotlib package.

Install packages without pip

Although pip is the recommended and easy tool for installing packages in Python. But you can also install packages manually and install those without using pip command.

To install package manually without pip, you need to download the source code of that package from the package repository. Then run the setup.py file.

install-multiple-packages-without-pip

Let’s say we want to install package or library requests manually to do that:

  • Go to the package repository site at https://pypi.org/project/requests/
  • Download the package source code in a zip or tar.gz format
  • Extract the downloaded file. You will see a file called setup.py
  • Now open your terminal or cmd and navigate to the extracted directory
  • Finally, run the following command to install the package
  • python setup.py install

The command will run the setup.py script and install the package in your python environment.

Also Read:  Jupyter Notebook: 500 : Internal Server Error

Install multiple versions of same package

While working in a Python project, sometimes you need to use multiple versions of the same package. This is required while testing any python code written earlier using any library. But now that library is updated with different functions.

You can install multiple versions of same package using pip command easily. But you need to install each version in different virtual environment.

FAQs

Where does pip install packages?

The location of the installed packages depends on the configuration of your Python environment and the operating system you are using. For example, I am using windows operating system and I have installed Python in D:\ drive.

By default, pip installs packages in the site-packages directory of the Python installation. The location of the site-packages directory can vary depending on the operating system and the way Python was installed ((in my case inside D:\ drive)).

You can use the pip show command to find out where a package is installed. For example, to find out the location where numpy package is installed, you can run:

pip show numpy

This command will show information about the package, including the location where it is installed.

where-does-pip-install-packages

How does pip install packages?

When you run pip command to install any package, it passes through the following steps one by one:

  1. First connects to the Python Package repository (PyPI) or another package repository specified in the package configuration.
  2. Then find and downloads its source code with its dependencies specified in the package metadata.
  3. Next, build the package and its dependencies
  4. Finally, Install the package by copying the built files to the appropriate location in the Python environment, typically the site-packages directory.
Also Read:  pip is not recognized as an internal or external command

Throughout this process, pip also perform various checks and validations to ensure that the package and its dependencies are compatible with the Python environment you are using.

Entire Python packages list

To see all the installed libraries you installed for a Python environment, you just need to run below code in Python.

help("modules")

Leave a comment