Jan 14 2025 · Karol Falkowski

How to install packages in Python

Python packages for data visualization banner.

If you have ever worked on a Python project, I'm sure you have struggled with package installation at least once. Python is a great programming language, but many people forget to mention how tricky package installation can be. There are many package manager tools, like pip, conda, poetry, mamba, and many more. In this article, I will focus on my favorite one — pip. Let's start!

Introduction

You are here to get information about installation packages in Python, but why install packages when you can write code yourself? I hasten to explain!

All of packages are really just code which other users wrote before you. They spent their time creating specific functions for example to create charts (like matplotlib) and they shared their work with the Python community to make your work a lot easier.

Now, when you know what the reason for package installation is, I will explain to you how you can do that using pip as a package manager tool.

Set up a virtual environment

Unfortunately, pip doesn't have its environment management so you need to use venv.

Create a virtual environment

If you don't have your environment yet, create it using the following command:

python -m venv venv_name

If you have installed Python3 use this command:

python3 -m venv venv_name

Activate the virtual environment

When you already have the virtual environment, use this command to activate it:

source venv_name/bin/activate

Once it's activated, the terminal will display the environment name like this: The terminal with activated virtual environment.

Deactivate the virtual environment

After you did everything you want in your environment, use the following command to deactivate it:

deactivate

Operations on packages

Install a single package

Use the following command to install any package:

pip install <package_name>

Sometimes packages have different install and import names. In place of package_name you need to insert the install name of the package.

Example:

pip install pandas

Install a specific version of package

You can install a specific version of the package for example when the newest version doesn't fit your needs. Here's a command you use to do it:

pip install <package_name>==<version>

Example:

pip install pandas==2.2.2

Install packages from requirements.txt file

If you have a requirements.txt file with a list of all the packages you need, you can install all of them at once using the following command:

pip install -r requirements.txt

Check installed packages

When you don't know if you already have a specific package, you can check it by displaying the list of installed packages. Use this command:

pip list

Example: List of the installed packages.

Update a package

To update an installed package to the latest version use the following commnad:

pip install --upgrade <package_name>

Example:

pip install --upgrade pandas

Uninstall a package

If you installed the wrong package or you just don't need it anymore you can uninstall it using the following command:

pip uninstall <package_name>

Example:

pip uninstall pandas 

Operations on packages in Jupyter Notebook

You can also manage your packages in Jupyter Notebook. All you have to do is add a piece of code before every command you wrote in the terminal:

import sys
!{sys.executable} -m <your_command>

Examples

Install a single package

import sys
!{sys.executable} -m pip install pandas 
Install pandas in Jupyter Notebook.

Uninstall the package

This time the case is a bit different. When you try to uninstall the package according to the pattern, this will happen:

Wrong way to uninstall pandas in Jupyter Notebook.

The cell is in an infinite process because you can't confirm the continuation of the process. To avoid this situation, use the following code:

import sys
!{sys.executable} -m pip uninstall pandas --yes

--yes allows to skip the confirmation prompt.

Uninstall pandas in Jupyter Notebook.

Operations on packages in MLJAR Studio

By the way, there is a powerful tool for data anysts named MLJAR Studio. It allows you to save a lot of time and effort during programming thanks to it code snipes and AI Assistant. One of its best features is that you can install all necessary packages with only one cick!

Packages installation in MLJAR Studio

Are you interested? Get more information here: https://mljar.com/.

Conclusion

Managing Python packages can be tricky, but using pip makes it much easier! This article covered how to create virtual environments and handle packages in both the terminal and Jupyter Notebooks. With these tips, you’ll keep your projects organized and avoid package issues.

Happy coding, and good luck with your Python journey! 😊

Become a Data Science wizard, today!

Forget about Python problems, just do your work.

MLJAR Studio