How to Install Python Packages Using pip (Beginner’s Guide with Examples)

Hand-drawn educational infographic explaining how to install Python packages using pip, activate a virtual environment, verify packages and use requirements.txt.
Learn how to install and manage Python packages with pip on Windows, macOS and Linux.

Python includes many useful features, but you will often need additional libraries to build real-world projects. For example, Requests helps you work with HTTP APIs, NumPy supports numerical computing, and Pandas helps you analyse data. You can install these third-party libraries with pip, Python’s standard package installer.

In this beginner-friendly guide, you will learn how to install Python packages using pip on Windows, macOS and Linux. We will also cover upgrading and uninstalling packages, checking installed versions, using a requirements.txt file, and fixing common installation errors. The examples work in the VS Code terminal as well as a regular command-line terminal.

What Is pip in Python?

pip is a command-line tool for installing and managing Python packages. It can download packages from the Python Package Index (PyPI), resolve their dependencies, and install them into the Python environment you are using.

A package is reusable code that adds functionality to a Python project. You do not need to write every feature yourself: you can install a suitable package and import it into your program.

Package Common use
requests Making HTTP requests and working with APIs
numpy Numerical calculations and arrays
pandas Working with tables and datasets
matplotlib Creating charts and visualisations
flask Building lightweight web applications

Before You Install a Python Package

Make sure Python is installed and that you can open a terminal. Check your Python version and pip availability with the commands for your operating system.

Windows

py --version
py -m pip --version

macOS and Linux

python3 --version
python3 -m pip --version

If the pip command displays a version and a path, pip is available for that interpreter. If it is missing, see the troubleshooting section below. On some systems, the Python executable may be named python instead of python3.

Step 1: Open Your Python Project in VS Code

  1. Open Visual Studio Code.
  2. Select File → Open Folder and choose your Python project folder.
  3. Open Terminal → New Terminal.
  4. Check that the terminal is running in your project directory.

If you have not installed Python or configured VS Code yet, follow our guide: How to Install Python and Set Up VS Code on Windows.

Step 2: Create and Activate a Virtual Environment (Recommended)

Before installing packages, create a virtual environment for your project. It keeps the project’s dependencies separate from other Python projects and helps avoid version conflicts. If you already created a .venv environment, activate it and continue to Step 3.

Create the environment

On Windows, run:

py -m venv .venv

On macOS or Linux, run:

python3 -m venv .venv

Activate it on Windows PowerShell

.\.venv\Scripts\Activate.ps1

Activate it on Windows Command Prompt

.venv\Scripts\activate.bat

Activate it on macOS or Linux

source .venv/bin/activate

When activation succeeds, your terminal prompt usually displays (.venv). In VS Code, you can also select the environment using Python: Select Interpreter from the Command Palette.

For screenshots, activation details and more troubleshooting, read How to Create a Python Virtual Environment in VS Code (Windows, macOS & Linux).

Step 3: Install Your First Python Package Using pip

Let’s install requests, a popular package for sending HTTP requests. With your virtual environment activated, run:

python -m pip install requests

Wait for pip to download the package and its dependencies. A successful installation ends with a message indicating that the packages were installed or are already satisfied.

Why use python -m pip instead of only pip? It runs pip through the Python interpreter named by python. This helps ensure that the package is installed into the same environment that will run your code.

If you are not using an activated virtual environment, use the platform’s Python launcher explicitly:

  • Windows: py -m pip install requests
  • macOS/Linux: python3 -m pip install requests

Step 4: Verify That the Package Is Installed

Use pip to display information about the package:

python -m pip show requests

You can also list packages installed in the active environment:

python -m pip list

Now test the package from Python. Create a file named test_requests.py and add:

import requests

print("Requests version:", requests.__version__)
print("Package imported successfully!")

Run the file from the terminal:

python test_requests.py

If the version appears and there is no import error, the package is installed in the Python environment running your program.

Step 5: Install a Specific Version of a Package

Sometimes a project requires a particular package version. You can specify the version with two equals signs:

python -m pip install requests==2.32.3

You can also set a version range. For example:

python -m pip install "requests>=2.32,<3"

Use the version required by your project or course. The version shown above is an example of version-specifier syntax, not a recommendation to pin every project to that exact release.

Step 6: Upgrade a Python Package

To upgrade an installed package to the latest version compatible with your Python environment, use --upgrade:

python -m pip install --upgrade requests

To upgrade pip itself inside the active environment, run:

python -m pip install --upgrade pip

Review project compatibility before upgrading dependencies in an existing application. A newer package version can sometimes introduce changes that require code updates.

Step 7: Uninstall a Python Package

If your project no longer needs a package, remove it with:

python -m pip uninstall requests

pip asks you to confirm the removal. Enter y when you want to continue. To skip the confirmation prompt, use:

python -m pip uninstall -y requests

Step 8: Install Multiple Packages Using requirements.txt

When a project uses several packages, listing them in a requirements.txt file makes setup easier. Create a file named requirements.txt in your project folder and add one package requirement per line:

requests
numpy
pandas
matplotlib

With the project’s virtual environment activated, install everything in the file using:

python -m pip install -r requirements.txt

You can generate a requirements file from the packages currently installed in your active environment:

python -m pip freeze > requirements.txt

This records installed package versions, which can help reproduce an environment. Remember that pip freeze records packages in the active environment, so use a dedicated project environment rather than an environment containing unrelated packages.

Common pip Commands Cheat Sheet

Task Command
Check pip version python -m pip --version
Install a package python -m pip install requests
Install a specific version python -m pip install requests==2.32.3
Upgrade a package python -m pip install --upgrade requests
Uninstall a package python -m pip uninstall requests
Show package details python -m pip show requests
List installed packages python -m pip list
Save installed packages python -m pip freeze > requirements.txt
Install from requirements python -m pip install -r requirements.txt
Check dependency issues python -m pip check

These commands use the active python interpreter. On Windows outside a virtual environment, you can usually replace python with py; on macOS and Linux, use python3 when that is the command for your installation.

Common pip Installation Errors and How to Fix Them

Error or symptom What to try
pip is not recognized or pip: command not found Use py -m pip on Windows or python3 -m pip on macOS/Linux instead of relying on the standalone pip command.
No module named pip Try py -m ensurepip --upgrade on Windows or python3 -m ensurepip --upgrade on macOS/Linux, if supported by that Python installation.
No matching distribution found Check the package name, Python version compatibility, platform support and internet connection. Confirm that the package exists on the package index you are using.
Permission denied or access error Prefer installing inside a virtual environment. Avoid using administrator or sudo commands to force a global installation.
ModuleNotFoundError after installation Check that VS Code selected the same interpreter where you installed the package. Run python -m pip show package_name in that environment.
externally-managed-environment Your operating system may manage the system Python installation. Create and activate a virtual environment, then install the package there instead of modifying system Python.
Download or connection timeout Check your internet connection, proxy or firewall settings, then retry. On managed networks, ask the administrator about the approved package index.

For more help with environments and interpreter selection, see Python Interpreter Not Found in VS Code? 7 Ways to Fix It.

Frequently Asked Questions

Is pip installed automatically with Python?

Many standard Python installations include pip, but some operating-system or custom installations may package it separately. Check with python -m pip --version or the equivalent command for your system.

What is the difference between pip and PyPI?

pip is the tool that installs and manages packages. PyPI, the Python Package Index, is the public repository from which pip commonly downloads Python packages.

Should I install Python packages globally or in a virtual environment?

For project development, use a virtual environment. It isolates dependencies and helps prevent one project’s package changes from affecting another project or the operating system’s Python installation.

How do I install a package in VS Code?

Open your project in VS Code, select the intended Python interpreter, open the integrated terminal, activate the project’s virtual environment, and run python -m pip install package_name.

How can I see all installed Python packages?

Run python -m pip list in the environment you want to inspect. It displays installed package names and versions.

Can I install multiple Python packages at once?

Yes. You can list package names in one command, such as python -m pip install requests numpy pandas, or place them in a requirements.txt file and install them with python -m pip install -r requirements.txt.

Is pip the same as importing a Python module?

No. pip installs a distribution package into an environment. Your Python code then imports the module or modules provided by that package. The package name and import name are not always identical.

Conclusion

pip makes it straightforward to add third-party functionality to Python projects. You now know how to check pip, install a package, verify it, choose a specific version, upgrade or uninstall it, and manage project dependencies with requirements.txt.

For reliable project setups, activate a virtual environment before installing packages and make sure VS Code is using that same interpreter. Practise by installing requests and running the small import example in this guide.

Official References

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top