Python libraries are installed using pip, the package manager that comes with Python

A Python library is a collection of code someone else wrote that you can use in your own programs. Instead of writing everything from scratch, you read a library and import it into your project. The tool that downloads and installs these libraries is called pip, and it comes built into Python on most systems.

The basic command is straightforward: you open your terminal or command prompt and type pip install libraryname. Python then downloads the library from PyPI (the Python Package Index, a central repository of libraries) and puts it where your Python installation can find it. After that, you can use the library in your code by writing import libraryname at the top of your script.

The process is the same whether you are on Windows, Mac, or Linux, though the terminal process you use differs slightly. Most beginners run into one of three problems: pip is not found, the library installs but Python cannot find it, or you install it in the wrong Python version. All three have straightforward fixes.

Key Takeaways

  • Install libraries by opening your terminal and typing pip install libraryname, where libraryname is the exact name of the library on PyPI.
  • Check that pip is installed by typing pip --version; if it is not found, you may need to reinstall Python or use python -m pip install instead.
  • Use pip list to see all libraries you have already installed, and pip show libraryname to check the version of a specific library.
  • Virtual environments let you install libraries for one project without affecting others, created with python -m venv foldername and activated before installing.
  • The library name in the install command is not always the same as the import name; check PyPI or the library's documentation if import libraryname fails after installation.

Checking whether pip is installed on your system

Before you install anything, confirm that pip exists on your computer. Open your terminal (on Mac or Linux) or Command Prompt (on Windows) and type pip --version. If pip is installed, you will see a version number like pip 23.1.2. If you see "command not found" or "pip is not recognized", pip is not in your system path, and you need to fix that first.

On Windows, the most common reason is that Python was not added to your PATH during installation. Reinstall Python and make sure you check the box that says "Add Python to PATH" during setup. On Mac or Linux, try typing python3 --version to confirm Python 3 is installed, then use python3 -m pip --version instead of just pip --version. If that works, use python3 -m pip install for all future installs on that machine.

Installing a library with pip

Once pip is working, installing a library takes one command. Open your terminal and type pip install requests (replacing "requests" with whatever library you need). Pip will read the library and any other libraries it depends on, then install them all. You will see output showing the read progress and a message like "Successfully installed requests-2.31.0" when it finishes.

The library name in the install command must match exactly what appears on PyPI. If you are unsure of the exact name, search for it on pypi.org. Some libraries have hyphens in their name on PyPI but underscores when you import them, or vice versa. For example, you install "Pillow" with pip install Pillow, but import it with from PIL import Image. The PyPI page for each library shows both the install name and the correct import statement.

After installation, you can use the library in your Python scripts. At the top of your file, write import libraryname (or the correct import name from the documentation), and then use the functions and classes the library provides. If Python says "ModuleNotFoundError" when you try to import, see the troubleshooting section below.

Understanding virtual environments and why they matter

A virtual environment is a separate folder on your computer where Python installs libraries just for one project. Without it, all libraries you install go into your main Python installation, and different projects can conflict if they need different versions of the same library. Virtual environments prevent that by keeping each project's libraries isolated.

Create a virtual environment by opening your terminal in the folder where your project lives and typing python -m venv venv. This creates a folder called "venv" (you can name it anything). Then set up it by typing source venv/bin/set up on Mac or Linux, or venv\Scripts\set up on Windows. Your terminal prompt will change to show the environment is active. Now when you run pip install, libraries go into that folder instead of your main Python installation.

When you are done working on the project, type deactivate to exit the virtual environment. The next time you work on that project, set up it again with the same command. This is standard practice for any real Python project, and most Python developers use virtual environments for everything they write.

Viewing and updating installed libraries

To see all the libraries you have installed in your current environment, type pip list. This shows the library name and version number for everything. To check the version of one specific library, type pip show requests (replacing "requests" with the library name). This also shows where the library is installed and what other libraries it depends on.

To update a library to a newer version, type pip install --upgrade libraryname or the shorter pip install -U libraryname. Pip will read and install the latest version. Most of the time you do not need to update libraries unless you are fixing a bug or need a new feature, but security updates are worth installing promptly.

Fixing common installation problems

If you see "ModuleNotFoundError: No module named X" when you try to import a library you just installed, the most likely cause is that you installed it in a different Python version than the one running your script. If you have both Python 2 and Python 3 on your computer, pip might point to Python 2 while your script uses Python 3. Try typing python3 -m pip install libraryname instead, then run your script with python3 scriptname.py.

Another common problem is installing a library while outside a virtual environment, then trying to use it inside one. If you activated a virtual environment after installing, the library is not in that environment. set up the environment first, then install the library again.

If pip says "Permission denied" on Mac or Linux, do not use sudo to force the install. Instead, check that you are in a virtual environment (you should be). If you are not, create one and install there. Using sudo installs to the system Python, which can break your operating system's own Python scripts.

Installing specific versions and managing dependencies

Sometimes you need a specific version of a library because your code was written for that version. Type pip install libraryname==2.5.1 (with two equals signs) to install version 2.5.1 exactly. You can also use pip install libraryname>=2.0 to install version 2.0 or any newer version, or pip install libraryname<3.0 to install any version before 3.0.

When you have a project with many libraries, you can save a list of them to a file called requirements.txt by typing pip freeze > requirements.txt. This file lists every library and its version. Later, you or someone else can install all of them at once by typing pip install -r requirements.txt. This is how Python projects share their dependencies with other developers.

Frequently Asked Questions

What is the difference between pip and conda?

Pip is the standard Python package manager and works with libraries on PyPI. Conda is an alternative that comes with Anaconda, a Python distribution aimed at data science. For most web development and general Python work, pip is what you need. Conda is useful if you work with scientific libraries like NumPy or Pandas and want pre-compiled versions.

Can I install multiple libraries at once?

Yes. Type pip install library1 library2 library3 to install several at once, separated by spaces. You can also install from a requirements.txt file with pip install -r requirements.txt, which is the standard way to set up a project with many dependencies.

What happens if I uninstall a library I need?

Your script will fail with a ModuleNotFoundError when it tries to import that library. straightforward reinstall it with pip install libraryname. If you accidentally deleted your virtual environment folder, create a new one and reinstall all libraries from your requirements.txt file.

Do I need to restart Python after installing a library?

If you are running Python in an interactive shell or Jupyter notebook, yes — restart the kernel or close and reopen the shell. If you are running a script from the command line, no — just run the script again after installing. The new library will be available.

Why does my library import with a different name than I installed it with?

Some libraries use a different name for the package (what you install) and the module (what you import). For example, you install "Pillow" but import "PIL". Always check the library's documentation or its PyPI page to find the correct import statement. The PyPI page shows this clearly under "Installation" or "Usage".