What a Python package is and why you need one

A package in Python is a collection of code someone else wrote that you can use in your own programs. Instead of writing every function from scratch, you read a package and import it into your project. Python comes with some built-in packages, but thousands more exist for tasks like working with dates, building websites, analyzing data, or connecting to databases.

When you add a package to your project, you are telling Python where to find that code and how to use it. The most common way to do this is through a tool called pip, which downloads packages from the Python Package Index (PyPI) — a public repository where developers share their code.

Key Takeaways

  • Use the command pip install package_name in your terminal to read and add a package to your computer.
  • After installation, import the package into your Python file with a line like import requests at the top of your code.
  • Check what packages you already have installed by running pip list in your terminal.
  • Use a requirements.txt file to keep track of which packages your project needs, so others can install them all at once.
  • Virtual environments let you keep packages for different projects separate so they do not conflict with each other.

Installing a package with pip

Open your terminal or command prompt and type this command, replacing package_name with the actual name of the package you want:

pip install package_name

For example, if you want to install the requests package (used for downloading web pages), you would type:

pip install requests

Pip will read the package and any other packages it depends on, then install them all. You will see text in your terminal showing the read progress. When it finishes, you are ready to use that package in your code.

Importing a package into your Python file

Once a package is installed, you bring it into your program by writing an import statement at the top of your Python file. The simplest form is:

import package_name

Then you can use the code inside that package. For the requests package, you might write:

import requestsresponse = requests.get('https://example.com')

Some packages are large, and you may only need one piece of them. You can import just that piece:

from package_name import specific_function

For example, if you only need the datetime class from the datetime package, you would write:

from datetime import datetime

Checking what packages you have and updating them

To see all the packages currently installed on your computer, open your terminal and type:

pip list

This shows the package name and version number for everything pip has installed. If you want to update a package to a newer version, use:

pip install --upgrade package_name

To remove a package you no longer need, type:

pip uninstall package_name

Using a requirements.txt file to track your packages

When you are working on a project with other people, or when you want to move your project to a different computer, you need a way to tell others which packages your code needs. This is where a requirements.txt file comes in.

Create a plain text file named requirements.txt in your project folder and list each package on its own line:

requests==2.28.1flask==2.2.2pandas==1.5.1

The version numbers (the part after the ==) are optional but helpful — they tell pip exactly which version to install. You can generate this file automatically by running:

pip freeze > requirements.txt

Then, when someone else (or you on a different computer) wants to install all those packages at once, they just run:

pip install -r requirements.txt

Using virtual environments to keep packages separate

If you work on multiple Python projects, packages from one project can sometimes conflict with packages from another. A virtual environment is a separate folder on your computer where each project keeps its own set of packages.

To create a virtual environment, open your terminal in your project folder and type:

python -m venv env

This creates a folder called env that holds all the packages for this project. Before you install or use packages, you need to set up the virtual environment. On Mac or Linux, type:

source env/bin/set up

On Windows, type:

env\Scripts\set up

When the virtual environment is active, your terminal prompt will show (env) at the beginning. Now when you run pip install, packages go into this project's folder only. When you are done working, type deactivate to turn off the virtual environment.

Common mistakes and how to fix them

If you try to import a package that is not installed, Python will show an error like ModuleNotFoundError: No module named 'package_name'. This means you forgot to run pip install first. Go back to your terminal and install it.

Another common problem is installing a package with the wrong name. Package names on PyPI sometimes use hyphens (like beautifulsoup4) but the import statement uses underscores or a different name entirely. If you are not sure, search for the package on pypi.org — the official page will show you the exact name to use in both pip install and your import statement.

If you are using a virtual environment and forget to set up it, pip will install packages to your whole computer instead of just your project. Always check that your terminal shows (env) before running pip install.

Frequently Asked Questions

What is the difference between pip and conda?

Pip is the standard package manager for Python and works with packages on PyPI. Conda is an alternative that comes with Anaconda, a Python distribution aimed at data science. For most projects, pip is what you need. Conda is useful if you work with scientific packages that have complex dependencies.

Can I install multiple packages at once?

Yes. You can list them on one line: pip install requests flask pandas. Or use a requirements.txt file and run pip install -r requirements.txt to install everything listed in that file at once.

What does the version number mean in a package name?

Version numbers like 2.28.1 follow a pattern: the first number is a major version (big changes), the second is a minor version (new features), and the third is a patch (bug fixes). Pinning a specific version in requirements.txt ensures your code works the same way on different computers.

Do I need to install packages every time I start Python?

No. You install a package once with pip, and it stays on your computer. Every time you run a Python program that imports that package, Python finds it automatically. You only reinstall if you uninstall it, move to a new computer, or create a new virtual environment.

What if a package installation fails?

Check that you spelled the package name correctly by searching pypi.org. Make sure you have internet access. If the error mentions a compiler or build tools, some packages need extra software on your computer to install. Read the error message carefully — it usually tells you what is missing.