Himanshu SharmaSoftware engineer
Let’s talk

Python Tutorial · Lesson 51 of 70

Python pip and virtual environments for beginners

Learn what pip and venv do, how to install third-party packages into an isolated environment, and how requirements.txt shares the exact setup.

12 minModules and packagesUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonCreate and activate a virtual environment, install a package with pip, and save and restore the list of packages for a project.

After this lesson you can install a package from the internet safely, without disturbing the rest of your computer, and you can share your project's package list with someone else.

Two kinds of modules

So far you have used your own modules and the standard library. Thousands of other useful tools are written by other people, for example for working with web requests, spreadsheets or data tables. These are called third-party packages. They are published on the Python Package Index, known as PyPI, and installed with a tool called pip.

The commands in this lesson run in a terminal, not inside Python. They are shown without output because the results depend on your computer and the network.

Checking pip

pip comes with modern Python. The safest way to run it is through the Python you intend to use:

bash
python3 -m pip --version

Writing python3 -m pip instead of plain pip avoids a common problem where pip belongs to a different Python from the python3 you run.

Installing a package

To install, say, the popular requests package:

bash
python3 -m pip install requests

Then you can import requests in your code. You can also install a specific version, upgrade, or remove:

bash
python3 -m pip install requests==2.31.0
python3 -m pip install --upgrade requests
python3 -m pip uninstall requests
python3 -m pip list

The version numbers above are only an example of the syntax, so check the package page for what is current.

Why you need virtual environments

If every package goes into one shared place, projects collide. Project A may need version 1 of a package, and project B version 2. On some systems the system Python also belongs to the operating system, and changing its packages can break tools. A virtual environment solves both problems. It is a folder containing its own copy of the Python tools and its own set of installed packages, used by one project only.

Creating and activating

Inside your project folder, create an environment. The conventional name is .venv:

bash
python3 -m venv .venv

Then activate it. On macOS and Linux:

bash
source .venv/bin/activate

On Windows PowerShell:

bash
.venv\Scripts\Activate.ps1

After activation your terminal prompt usually shows (.venv) at the start. From then on, python and pip refer to the environment. Install your packages as before and they go into .venv only. When you are finished:

bash
deactivate

You can check from inside Python whether an environment is active. The result below was produced outside any environment, so you would see True when run inside one:

python
import sys

print(sys.prefix != sys.base_prefix)
result
False

requirements.txt

How does a friend, or a server, get the same packages? You record them in a text file. After installing what you need, export the list:

bash
python3 -m pip freeze > requirements.txt

The file lists each package with its exact version, one per line. These two lines only illustrate the shape. Your own file will list whatever you installed:

text
requests==2.31.0
certifi==2023.7.22

Anyone can then create their own environment and install everything at once:

bash
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt

Version control

The .venv folder is large and specific to one machine and operating system. Do not commit it. Add its name to your .gitignore file, and commit requirements.txt instead.

Being careful with packages

Anyone can publish to PyPI, so check a package before installing it. Look at its project page, its age, how widely it is used, and spell the name carefully, since look-alike names exist. Never run pip install with sudo, and never install a package just because a random comment told you to.

Upgrading and keeping things tidy

Packages get updated. To see which ones in your environment have newer versions, you can ask pip, and then upgrade only the ones you want:

bash
python3 -m pip list --outdated
python3 -m pip install --upgrade requests

Upgrades can change behaviour, so after an upgrade run your program or tests before you save a new requirements.txt. If a project stops working after an update, the old file is your way back: delete the environment, create a fresh one and install from the saved list. Because an environment is cheap to rebuild, you should feel free to delete and recreate it whenever it seems confusing. That is one of the biggest practical benefits of having one per project.

Finally, install only what you need. Each extra package is more code that you depend on and must keep up to date.

Common mistakes

  • Installing without activating the environment, so the package lands in the system Python.
  • Using pip and python3 from different Pythons. Use python3 -m pip.
  • Committing the .venv folder to Git.
  • Forgetting to update requirements.txt after installing something new.
  • Copying a .venv to another computer. It will not work. Recreate it from requirements.txt.
  • Naming your file after the package, such as requests.py, which then shadows the real one.

Exercise

Write down, in order, the terminal commands to start a new project called shop-report that uses the requests package: create the folder, create and activate an environment, install the package, and record it in requirements.txt. Assume macOS or Linux.

Show one possible answer
bash
mkdir shop-report
cd shop-report
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install requests
python3 -m pip freeze > requirements.txt

Key points

  • pip installs third-party packages from the Python Package Index (PyPI).
  • A virtual environment is a private folder of packages for one project, created with python3 -m venv.
  • Activate the environment before installing, so packages go into the project and not into the system Python.
  • pip freeze lists installed versions, and a requirements.txt file lets anyone recreate the same setup.
  • Keep the environment folder out of version control. Commit requirements.txt instead.

Check yourself

1. What is pip?
2. Why use a virtual environment?
3. Which command creates a virtual environment named .venv?
4. What should you commit to version control?