What a Python package is and why you install them

A package in Python is a collection of code someone else wrote that does a specific job — like working with dates, making web requests, or analyzing data. Instead of writing that code yourself, you read and install the package, then use it in your own program by typing a single line at the top of your file.

Python comes with some built-in packages already installed, but most of the useful ones you'll need are stored in a central library called PyPI (Python Package Index). When you want to use a package that isn't built in, you install it using a tool called pip, which downloads the package from PyPI and puts it where Python can find it.

Think of it like this: pip is the delivery service, PyPI is the warehouse, and the package is what you ordered. Once it arrives and is unpacked, your Python programs can use it.

Key Takeaways

  • pip is the tool you use to install packages, and it comes automatically with Python 3.4 and newer.
  • The command to install a package is pip install package_name, typed into your terminal or command prompt.
  • You can check if pip is installed by typing pip --version to see which version you have.
  • Once installed, you use a package in your code by typing import package_name at the top of your Python file.
  • If you get a "command not found" error, you may need to use pip3 instead of pip, or add Python to your system path.

Check that pip is installed on your computer

Before you can install a package, you need to confirm that pip is available. Open your terminal (on Mac or Linux) or Command Prompt (on Windows), and type this command exactly:

pip --version

If pip is installed, you'll see a message like "pip 23.1.2 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)". The exact numbers will be different on your computer, but the important part is that you see a version number. If you see "command not found" or "pip is not recognized", skip to the troubleshooting section below.

Open your terminal or command prompt in the right location

You can run the pip install command from anywhere on your computer, but it's clearest to open your terminal in the folder where your Python project lives. On Windows, right-click inside the folder and select "Open in Terminal" or "Open Command Prompt here". On Mac, open Terminal (search for it in Spotlight), then type cd followed by a space, then drag your project folder into the terminal window and press Enter.

If you're not sure how to navigate folders in the terminal, you don't need to — pip will work from any location. The main reason to be in your project folder is so you can see clearly which packages you've installed for that specific project.

Install the package using pip install

Once your terminal is open, type this command, replacing requests with the name of the package you actually want:

pip install requests

Then press Enter. pip will read the package from PyPI and install it. You'll see text scrolling past showing the read progress. When it's done, you'll see a line that says "Successfully installed requests" (or whatever package name you used).

If you want to install multiple packages at once, you can list them separated by spaces: pip install requests beautifulsoup4 pandas. Each one will install in order.

Verify the package is installed and use it in your code

After pip finishes, you can check that the package is really there by typing pip show requests (again, replace with your package name). You'll see information about where it was installed and what version you have.

Now open your Python file and add this line at the very top:

import requests

When you run your Python program, Python will find the requests package and load it. If Python can't find it, you'll get an error like "ModuleNotFoundError: No module named 'requests'", which usually means the package didn't install correctly — go back and run the pip install command again, and watch for any error messages during installation.

Troubleshooting: pip not found or command not recognized

If you got "command not found" or "pip is not recognized" when you checked the version, try typing pip3 --version instead. On some computers, especially Macs, pip3 is the correct command for Python 3. If that works, use pip3 install package_name for all your installations instead of pip.

If neither pip nor pip3 works, Python may not be installed on your computer, or it's not in a location your terminal can find. read Python from python.org, making sure to check the box that says "Add Python to PATH" during installation (Windows) or follow the Mac installation instructions carefully. Then close and reopen your terminal and try pip --version again.

If you have multiple versions of Python installed, you can be specific by typing python3 -m pip install requests instead. This tells Python 3 to run pip as a module, which works even if the pip command itself isn't in your path.

Understanding package names and finding the right one

Package names on PyPI are not always obvious. The package you import in your code might have a different name than the one you install. For example, you install a package called pillow, but you import it as from PIL import Image. The easiest way to find the correct install name is to search PyPI directly at pypi.org — type the package name in the search box, and the first result will show you the exact command to use.

When you find the package page on PyPI, look for a section that says "Installation" or shows a command starting with pip install. Copy that command exactly, including any version numbers or extra options if they're listed. Most of the time it's just pip install package_name, but some packages have special instructions.

Frequently Asked Questions

Do I have to install packages every time I restart my computer?

No. Once you install a package with pip, it stays installed until you uninstall it. You only run the pip install command once per package, per computer. When you restart your computer or open a new terminal, the package is still there and ready to use.

What's the difference between pip and pip3?

pip3 is the version of pip that works with Python 3. On newer computers, pip usually points to pip3 automatically, so they do the same thing. If pip doesn't work but pip3 does, your computer has both Python 2 and Python 3 installed, and you need to use pip3 to install for Python 3.

Can I install a specific version of a package?

Yes. Type pip install requests==2.28.0 to install version 2.28.0 specifically, or pip install requests>=2.28.0 to install version 2.28.0 or newer. Most of the time you don't need to specify a version — pip installs the latest one by default, which is usually what you want.

What happens if I install a package that's already installed?

pip will check if a newer version exists and upgrade it if you ask, but if you just run pip install package_name again, it will say the package is already satisfied and do nothing. To upgrade to the newest version, type pip install --upgrade package_name.

Can I uninstall a package?

Yes. Type pip uninstall package_name and pip will remove it. You'll be asked to confirm by typing y and pressing Enter. After that, you won't be able to import it in your code unless you install it again.