Programming with Python › Python Setup and First Programs › Day 43

Day 43: Installing Python and Virtual Environments

Day 43 of 365 — Installing Python and Virtual Environments

After this lesson you will have a working Python you can run from the terminal, and you will be able to create, activate, and reproduce an isolated virtual environment for any project — the foundation every reproducible AI project stands on.

Course
Programming with Python
Category
Python Setup and First Programs
Reading time
≈ 40 min
Practical time
≈ 30 min
Lesson duration
1h 10m
Last verified
2026-07-12

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/programming-with-python/day-043-installing-python-and-virtual-environments

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/programming-with-python/day-043-installing-python-and-virtual-environments
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters


The idea in plain language

Diagram: system-wide package installation compared with per-project environments


Historical background


What it is — and what it is not

Diagram: what a virtual environment isolates and what it does not

A virtual environment is:

It is not:


Why it was created and what problems it solves


How it works

Two ways to run code

Diagram: how Python runs a script through the interpreter, and the read-eval-print loop of the REPL

Finding which Python you are running

Diagram: how activating a virtual environment changes PATH

Creating and using an environment

Diagram: the three steps of setting up a virtual environment

TaskCommandWhat it does
Createpython3 -m venv .venvBuilds a folder with a private packages area
Activatesource .venv/bin/activateRewires this shell so python/pip mean the venv’s
Confirmwhich pythonShould print a path ending .venv/bin/python
Installpip install <name>Into the venv only, never globally
Pinpip freeze > requirements.txtEvery package and its exact version
Rebuildpip install -r requirements.txtExactly those versions, elsewhere
Turn offdeactivateRestores the shell to the global Python

Diagram: one shared Python with two projects that each keep their own packages in separate virtual environments

pip, PyPI, and requirements.txt

Diagram: pinning an environment and rebuilding it elsewhere


An everyday analogy

A shared workshop with personal toolboxes.

The workshopPython
The building and its power supplyThe interpreter
The communal tool rackThe system packages
Your own locked toolboxA virtual environment
Taking your toolbox to the benchActivating
A written inventory of its contentsrequirements.txt
Rebuilding an identical box from the listpip install -r
Putting your box awaydeactivate

Examples in practice

Diagram: diagnosing a mismatch between pip and python


Implications: security, privacy, performance, scalability, and cost

Diagram: the risk of installing packages into the system Python


Alternatives: free, open source, and commercial

Diagram: managing Python versions compared with managing packages

ToolTypeWhere it fits
venvFree, standard libraryThe default. Nothing to install.
uvFree, open sourceSame job, dramatically faster; increasingly the default
PoetryFree, open sourceDependency resolution and packaging together
conda / mambaFree, open sourceScientific stacks, including non-Python pieces like CUDA
pyenvFree, open sourceManaging several Python versions, not packages
DockerFree, open sourceWhen the whole environment must travel, not just packages

TermWhat it actually is
InterpreterThe program that runs your Python code
Virtual environmentA private package directory for one project
ActivatePrepend the environment’s bin to PATH, for this shell
pipThe installer
PyPIThe public index it downloads from
requirements.txtThe pinned list of what was installed
pyenvManages Python versions, not packages

When to use it — and when not to

Diagram: the setup checklist for a new Python project


The AI connection

Knowledge check

quiz:
  question: "A package installs successfully with pip, but importing it in the very next command raises ModuleNotFoundError. What is the most likely cause?"
  options:
    - "pip and python resolve to different installations, so the package went somewhere the interpreter is not looking"
    - "The package requires a newer Python version than the one installed"
    - "The install completed but the package index served a corrupted archive"
    - "The package must be added to requirements.txt before it can be imported"
  answer_index: 0
  explanation: "pip and python are two separate programs found on PATH. If an environment is not activated — or is only half activated — pip may install into one location while python looks in another. Running which pip and which python side by side shows the mismatch immediately."

Hands-on exercise

Create an environment, prove the isolation, then rebuild it from a pinned file. Worked through fully in the Day 43 lab directory.

What you are doingCommand
Find your interpreterwhich python3 and python3 --version
Create an environmentpython3 -m venv .venv
See that it is not on yetwhich python
Activate itsource .venv/bin/activate
Confirm it workedwhich python — now inside .venv
Install somethingpip install requests
Pin itpip freeze > requirements.txt
Leave, and rebuild elsewheredeactivate, then a fresh venv and pip install -r

Expected output

/usr/bin/python3
Python 3.12.4
/Users/you/project/.venv/bin/python
Successfully installed requests-2.32.3
requests==2.32.3

Validate your work

Troubleshooting

Common mistakes


Practice assignment


Extension challenge


Quiz

Q1. What is the Python "interpreter"?

  1. A website that translates Python into other programming languages
  2. The program (run as python3) that reads your code and executes it
  3. A file that lists which packages a project needs
  4. The folder where installed packages are stored
Show answer

Answer: B. The program (run as python3) that reads your code and executes it

Python is both a language and the program that runs code written in it. That program is the interpreter, usually invoked as python3, and it can run a whole script file or accept lines interactively in the REPL.

Q2. What is the difference between running a script and using the REPL?

  1. The REPL runs faster because it skips the interpreter
  2. A script needs a virtual environment but the REPL does not
  3. A script runs a file; the REPL evaluates one line at a time
  4. Scripts use python3 while the REPL uses a completely different program
Show answer

Answer: C. A script runs a file; the REPL evaluates one line at a time

Both feed the same interpreter. Handing it a file (python3 hello.py) runs the whole file top to bottom; starting python3 with no file gives the REPL — read, evaluate, print, loop — where you type one line at a time.

Q3. Why does this course prefer the command python3 over python?

  1. python3 runs three times faster than python
  2. python is only available on Windows
  3. python cannot create virtual environments
  4. python often meant Python 2; python3 names Python 3
Show answer

Answer: D. python often meant Python 2; python3 names Python 3

During the long overlap between Python 2 and Python 3, many machines shipped both, with python meaning the old Python 2. python3 removes the ambiguity and works portably across systems.

Q4. What problem does a virtual environment primarily solve?

  1. Projects keep their own package versions without conflict
  2. It makes Python code run faster at runtime
  3. It encrypts your source code for security
  4. It replaces the need to install Python at all
Show answer

Answer: A. Projects keep their own package versions without conflict

Without isolation, all projects share one global pile of packages, so one project needing a different version of a library breaks another. A venv gives each project a private package folder, ending those collisions.

Q5. After running source .venv/bin/activate, how can you confirm the environment is active?

  1. Restart the computer and check again
  2. Run which python and see a .venv path
  3. Check that the file requirements.txt exists
  4. Run python3 --version and confirm it prints 3.x
Show answer

Answer: B. Run which python and see a .venv path

Activation rewires the current shell so python and pip point inside the venv. which python then prints a path inside .venv, and most shells also show a (.venv) marker in the prompt. sys.prefix pointing inside .venv is the definitive proof.

Q6. What is the relationship between pip and PyPI?

  1. They are two names for the same program
  2. pip is the online repository and PyPI is the installer
  3. pip is the installer; PyPI is the repository it downloads from
  4. PyPI is a virtual environment manager and pip creates environments
Show answer

Answer: C. pip is the installer; PyPI is the repository it downloads from

pip is the tool that installs packages, and by default it fetches them from PyPI, the public online index of open-source Python libraries — the Python-specific version of the package-manager idea from Day 13.

Q7. What does a requirements.txt file give you?

  1. A backup copy of your source code
  2. Automatic activation of the virtual environment
  3. A faster interpreter for large projects
  4. A text recipe of exact packages and versions
Show answer

Answer: D. A text recipe of exact packages and versions

pip freeze writes every installed package and its exact version to requirements.txt. Running pip install -r requirements.txt in a fresh venv rebuilds the identical set of packages, which is what makes an environment reproducible across machines and time.

Q8. You need Python 3.11 for one project and Python 3.13 for another on the same machine. Which tool is designed for that?

  1. pyenv, which switches between Python versions
  2. pip, by installing both versions
  3. venv, by creating two environments
  4. requirements.txt, by listing both versions
Show answer

Answer: A. pyenv, which switches between Python versions

A venv isolates packages but shares one base interpreter. Switching between different Python versions is a separate job handled by a version manager like pyenv, which installs and selects among multiple Python versions and pairs with venv rather than replacing it.

Glossary

interpreter
The program (run as python3) that reads Python code and executes it, either from a whole script file or one line at a time in the REPL.
REPL
The read-eval-print loop: Python's interactive prompt (>>>), where you type one line, it evaluates and prints the result, then waits for the next line.
script
A file of Python code, run all at once with python3 filename.py, as opposed to typing lines interactively in the REPL.
virtual environment
A lightweight, isolated folder that gives one project its own private set of installed packages, kept separate from the global Python and from other projects.
venv
Python's built-in module for creating virtual environments, used as python3 -m venv .venv; also a common name for the environment folder it creates.
activate
To turn on a virtual environment (source .venv/bin/activate), which rewires the current shell so python and pip point inside that environment; deactivate turns it off.
pip
Python's package installer: the tool that downloads and installs libraries into whichever environment is active, by default from PyPI.
PyPI
The Python Package Index, the public online repository of open-source Python libraries that pip downloads from by default.
package
A reusable library of Python code, published on PyPI and installed with pip, that adds functionality beyond the standard library.
dependency
A package that a project needs in order to run; a project's dependencies (and their exact versions) are what a virtual environment isolates and requirements.txt records.
requirements.txt
A plain-text file listing a project's packages and their exact versions, used with pip install -r requirements.txt to recreate the same environment elsewhere.
pyenv
A tool that installs and switches between multiple versions of Python on one machine; it manages Python versions and pairs with venv, which manages packages.
standard library
The large set of modules that ships with Python itself, available without installing anything from PyPI.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.