Installing Python and Virtual Environments
Set up a local Python installation and understand how a virtual environment isolates project packages.
Video coming to YouTube
Work through the full lesson and run its code while the video is being prepared for publication.
Understand the concept
To work outside this website, download Python from python.org/downloads for your operating system, follow its installer, then open a new terminal and verify the interpreter. Windows: py --version. macOS or Linux commonly use python3 --version. Commands can vary by installation and shell; follow the official installation guide for your platform.
A virtual environment is a project folder with its own Python executable and installed packages. In a local terminal, create one with py -m venv .venv on Windows or python3 -m venv .venv on macOS/Linux. The official Python guide shows activation; for example, PowerShell uses .venv\Scripts\Activate.ps1 and a POSIX shell uses source .venv/bin/activate. Then python -m pip --version checks the pip associated with that active interpreter.
Those are operating-system shell commands, not Python statements. The browser console below runs Pyodide; it cannot install Python onto your machine or create your local .venv. Here you will inspect the browser runtime with sys.version_info. On your own computer, run the setup commands in a terminal.
- Install from the official Python downloads page
- python -m venv creates an isolated local environment
- sys.version_info reports the running Python version
See it step by step
Read the code, predict the output, then compare it with the result.
01. Inspect the interpreter
import sys
print(sys.version_info.major)3The website runs Python 3; sys.version_info gives its major version without guessing the minor release.
02. Inspect a version pair
import sys
version_pair = (sys.version_info.major, sys.version_info.minor)
print(len(version_pair))
print(version_pair[0] == 3)2
TrueThe pair contains the major and minor numbers. The boolean check is true in this Python 3 course without assuming an exact minor release.
A closer look
Follow the reasoning, inspect each result, then try the suggested changes in the console below.
Find the interpreter that runs your code
A computer can have several Python interpreters: one installed system-wide, another installed by a package manager, and one inside each virtual environment. The command name you type in a terminal chooses one of them. After installation, check the version in a new terminal and then run a small script with that same command. Otherwise you may verify one Python and accidentally use another.
The browser console has its own interpreter. sys.version_info here describes that runtime, not your laptop. The code below checks a major version and a minimum version pair without assuming the minor version printed by your local installation. In a local terminal, use py --version on Windows or python3 --version where that command is available, then run the script with the matching command.
import sys
major_minor = sys.version_info[:2]
print(major_minor[0] == 3)
print(major_minor >= (3, 10))True
True- sys.version_info[:2] selects the major and minor version numbers.
- The website's Python 3 runtime meets the displayed minimum.
- Change the minimum to (99, 0) and predict the second line.
Think of each environment as a project boundary
A virtual environment gives a project its own interpreter context and package installation location. One project can use a package version without changing another project's packages. Create .venv inside a local project folder with py -m venv .venv on Windows or python3 -m venv .venv on macOS/Linux. Activation changes which python and pip commands a shell finds; it does not alter the website's browser runtime.
The dictionary below is a model of two project dependency lists, not a way to create environments in the console. It illustrates why separate package contexts matter: the same library name can map to different versions. After activating your local environment, use python -m pip --version to check which pip belongs to the selected interpreter before installing anything.
project_a = {"httpx": "0.27"}
project_b = {"httpx": "0.28"}
print(project_a["httpx"])
print(project_b["httpx"])
print(project_a is not project_b)0.27
0.28
True- The projects have separate dictionaries, just as environments separate installed packages.
- Changing one mapping does not update the other mapping.
- Change project_a's version and predict the three printed lines.
Know what belongs in a terminal
A shell command such as python -m venv .venv is not Python source code. Run it in a local terminal from the directory where the project should live. The folder it creates typically contains a pyvenv.cfg file and platform-specific executable paths. PowerShell and POSIX shells use different activation commands; use the official guide for your shell. If activation is blocked, you can invoke the environment's interpreter by its path instead.
The browser exercise cannot inspect or modify a local .venv. The Python below only builds a Path object that describes where one configuration file would be inside a local project; it does not create the folder. This distinction helps prevent a common setup mistake: seeing a plausible path in Python output is not proof that a local environment was installed.
from pathlib import Path
environment = Path(".venv")
config = environment / "pyvenv.cfg"
print(environment.name)
print(config.name)
print(config.parent == environment).venv
pyvenv.cfg
True- Path joins two names into a possible local file location.
- The parent relationship is true even though this code creates no files.
- Change the environment name to env and predict the first line.
Try it in Python
Edit the example and run it. Python starts in your browser the first time you click Run.
Python console
Ready to runNeed input()? Add one value per line
Your output appears here.
Need a hint?
sys.version_info has major and minor attributes. Return (sys.version_info.major, sys.version_info.minor). This task inspects the browser runtime; it does not run pip or venv in the browser.
Complete the task and select Check task to verify your code.
Quick quiz
Three questions. You can change your answers and try again.
Typical mistakes
Everyone meets these errors. See what causes them and how to fix them.
Typing a terminal command into Python code
python -m venv .venvimport sys
print(sys.version_info.major)What happens: SyntaxError in the browser editor: this is a shell command.
Use the browser editor for Python statements and a local terminal for setup commands.
Assuming a browser version is the local version
print('My laptop uses Python 3.13')import sys
print(sys.version_info.major, sys.version_info.minor)What happens: The claim may be false because the browser runtime is separate.
Inspect each interpreter in its own environment rather than assuming they match.