How to Install Python Packages with pip and requirements.txt

One of the main advantages of using Python is the vast number of packages and libraries that are available, which makes it easier to perform various tasks.

However, in order to use these packages, they need to be installed on your system.

In this tutorial, we will discuss two methods for installing Python packages: using pip and using requirements.txt.


Method 1: Installing Packages with pip

pip is the default package manager for Python and is used to install and manage packages.

To install a package with pip, you can use the following command:

pip install

For example, to install the NumPy package, you can run the following command:

pip install numpy

You can also install packages from a specific source, such as a local file or a URL, using the following command:

pip install -f

For example, to install the NumPy package from a local file, you can run the following command:

pip install numpy -f /path/to/numpy.tar.gz

Method 2: Installing Packages with requirements.txt

Another way to install Python packages is by using a requirements.txt file.

This file contains a list of all the packages that your project requires, along with their versions.

To install the packages listed in a requirements.txt file, you can use the following command:

pip install -r requirements.txt

Here’s an example of what a requirements.txt file might look like:

numpy==1.18.5
pandas==1.0.3
matplotlib==3.3.0

This method is especially useful when working on a team or when you need to set up a development environment on a new machine.

By using a requirements.txt file, you can easily ensure that everyone on your team has the same version of the packages installed, and you can also quickly set up a new environment without having to manually install each package.


Conclusion

In this tutorial, we discussed two methods for installing Python packages: using pip and using requirements.txt.

Both methods are easy to use and provide a convenient way to install and manage packages in Python.

Whether you’re working on a small project or a large team, these methods will help you get the packages you need to get your work done.