The fastest way to add a package in Python

The standard way to add a package in Python is to use pip, the package installer that comes built into Python. Open your terminal or command prompt, type pip install package_name, and pip downloads and installs the package for you. For example, to install the popular data analysis library NumPy, you would type pip install numpy. Pip handles finding the package on PyPI (Python Package Index), downloading the correct version, and placing it where Python can find it.

Before you install anything, check which version of Python you have and whether pip is already installed. Type python --version and pip --version into your terminal. If pip is not installed, you can install it by downloading get-pip.py from the official pip website and running it, though most modern Python installations include pip by default.

Key Takeaways

  • Use pip install package_name in your terminal to add a package; pip is the standard Python package manager and comes with most Python installations.
  • Check your Python and pip versions first by typing python --version and pip --version to confirm pip is ready to use.
  • If you work with multiple Python projects, use a virtual environment to install packages separately for each project instead of globally.
  • You can install a specific version of a package by typing pip install package_name==version_number, which prevents version conflicts between projects.
  • A requirements.txt file lets you list all packages your project needs, then install them all at once with pip install -r requirements.txt.

Installing packages globally versus in a virtual environment

When you run pip install without any special setup, pip installs the package globally on your computer. This works fine for learning or for packages you use in many projects. However, if you work on multiple Python projects, global installation can cause problems: one project might need version 1.0 of a package while another needs version 2.0, and you cannot have both installed globally at the same time.

A virtual environment solves this by creating a separate folder for each project where packages are installed independently. To create a virtual environment, navigate to your project folder in the terminal and type python -m venv env. This creates a folder called env (you can name it anything). Then activate it by typing source env/bin/activate on Mac or Linux, or env\Scripts\activate on Windows. Once activated, any package you install with pip goes into that environment only, not globally.

Most Python developers use virtual environments for every project, even small ones, because it keeps your computer clean and prevents version conflicts. When you are done working on a project, you can delete the virtual environment folder and all its packages disappear with it.

Installing specific versions and multiple packages

By default, pip install package_name installs the latest version. If you need a specific version — because your code was written for it, or because a newer version broke something — type pip install package_name==1.2.3 with the exact version number. You can also use pip install package_name>=1.0 to install version 1.0 or any newer version, or pip install package_name<2.0 to install any version below 2.0.

To install multiple packages at once, list them separated by spaces: pip install numpy pandas matplotlib. This saves time if you are setting up a new project with several dependencies.

For projects with many packages, create a file called requirements.txt in your project folder and list each package on a new line, with optional version numbers:

numpy==1.21.0 pandas>=1.3.0 matplotlib<4.0

Then install everything at once by typing pip install -r requirements.txt. This approach is standard in professional projects because it documents exactly what your code needs and makes it easy for other people to set up the same environment.

Checking what packages you have installed

To see all packages currently installed in your environment, type pip list. This shows the package name and version number for everything. If you want to see only packages you installed directly (not packages that other packages depend on), type pip list --user on some systems, though this does not work consistently across Windows, Mac, and Linux.

To check whether a specific package is installed and what version you have, type pip show package_name. This displays the version, where it is installed, what other packages depend on it, and what packages it depends on.

Troubleshooting common installation problems

If you get a "command not found" error when you type pip install, pip is either not installed or not in your system path. Try python -m pip install package_name instead, which runs pip as a Python module and often works when the pip command alone does not. If that also fails, download get-pip.py from pypa.io/get-pip.py, save it, and run python get-pip.py to install pip.

If you get a "permission denied" error on Mac or Linux, do not use sudo pip install — this installs the package with administrator privileges and can cause problems later. Instead, use a virtual environment, which lets you install without special permissions. If you must install globally without a virtual environment, use pip install --user package_name, which installs to your user folder instead of the system folder.

If pip says the package does not exist, check the spelling — package names on PyPI are case-sensitive and sometimes use hyphens or underscores in unexpected places. Search PyPI.org directly to find the exact name. If you are behind a corporate firewall or proxy, you may need to configure pip to use your proxy settings; contact your IT department for the proxy address and use pip install --proxy [user:passwd@]proxy.server:port package_name.

Using packages after installation

Once a package is installed, you use it in your Python code by typing import package_name at the top of your script. For example, after installing NumPy with pip install numpy, you can write import numpy in your code and use its functions. Some packages have shorter import names: NumPy is imported as import numpy as np by convention, and Pandas as import pandas as pd.

If a package is not found when you try to import it, check that you installed it in the same environment where you are running your code. If you created a virtual environment, make sure it is activated before you run your script. If you are using an IDE like Visual Studio Code or PyCharm, make sure the IDE is set to use the correct Python interpreter — it should point to the virtual environment folder, not your system Python.

Updating and removing packages

To update a package to the latest version, type pip install --upgrade package_name or the shorter pip install -U package_name. To remove a package you no longer need, type pip uninstall package_name. Pip will ask you to confirm before removing it.

If you want to update all packages in your environment at once, there is no single pip command that does this safely, because updating everything can break code that depends on specific older versions. Instead, update packages one at a time or use a tool like pip-review, which you can install with pip install pip-review and then run with pip-review --auto-upgrade.

Frequently Asked Questions

What is the difference between pip and conda?

Pip installs Python packages from PyPI. Conda is a separate package manager that comes with Anaconda, a Python distribution aimed at data science. Conda can install non-Python packages and handles some dependencies better, but pip is the standard for most Python projects. If you installed Python directly from python.org, you have pip. If you installed Anaconda, you have conda.

Can I install a package directly from GitHub instead of PyPI?

Yes. Type pip install git+https://github.com/username/repository.git to install directly from a GitHub repository. This is useful for packages that are not yet on PyPI or for installing development versions. You need Git installed on your computer for this to work.

What does it mean when pip says a package has unmet dependencies?

A package often depends on other packages to work. Pip usually installs these automatically, but sometimes a dependency cannot be installed because your Python version is too old or too new. Check the package documentation for which Python versions it supports, or try installing an older version of the package that supports your Python version.

How do I know which packages I actually need for my project?

Start by installing only what you know you need. As you write code and get import errors, install the missing packages. Once your project is working, type pip freeze > requirements.txt to save a list of everything you installed. You can then remove packages you are not actually using and keep only what matters.

Is it safe to install packages from PyPI?

PyPI is the official Python package repository and most packages are legitimate. However, anyone can upload to PyPI, so malicious packages do exist. Stick to well-known packages with many downloads and recent updates. Check the package page on PyPI.org to see when it was last updated and how many people use it. If a package has zero downloads and was last updated years ago, be cautious.