Installing Python and Virtual Environments
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.
Set up a local Python installation and understand how a virtual environment isolates project packages.
Example check: Worked and guided code samples are checked against CPython 3.13. Practice uses Pyodide 0.29.5 (Python 3.13) in your browser. How we check lessons
Video lesson: Installing Python and Virtual Environments
The preview is stored on this site. YouTube loads only when you press Play. Watch on YouTube
Jump to a chapter
Read the video transcript
Select a timestamp to watch that moment on YouTube.
- 00:00
The Python console on PythonLessonLab is useful because you can run examples without setting up your computer. Eventually you may want to keep project files locally, work with packages, and run programs from a terminal. For that, you need to identify a Python interpreter on your own machine. This lesson gives you a practical path: find the official download for your operating system, verify the installation, create a virtual environment inside a project folder, and check which interpreter and package installer you are using. We will also inspect the browser interpreter so you can see why its version may differ from your local one.
- 00:40
Some commands depend on your operating system and shell, so use the official instructions linked in the lesson when their exact behavior differs from the examples on screen. Keep the website console open for Python code, and a local terminal open for setup commands. Begin at the official Python downloads page and choose the instructions for your operating system. Do not assume that a command shown for someone else's system is the right command for yours. After installation, open a new local terminal. On many Windows installations, the Python launcher responds to py followed by two minus signs and version.
- 01:21
On many macOS and Linux systems, python three followed by two minus signs and version is the command to check. If one of these commands is not found, return to the official platform guide rather than guessing that the browser console can repair your local setup. The goal is to verify the interpreter you plan to use. A computer can have several Python interpreters installed at once. A package manager, system tools, and individual project environments may each provide one. The command that starts your script decides which interpreter actually runs it. That is why a successful version check is a starting point, not proof that every project uses the same Python.
- 02:05
Make a new folder for a local practice project and open your terminal there. Run the virtual environment module from the local Python interpreter. On Windows, the example command is py minus m venv dot venv. On macOS or Linux, the example is python three minus m venv dot venv. The first venv refers to Python's environment module. The second dot venv is the folder name we chose. This creates a directory inside the project with an environment-specific Python executable and places for project packages. You normally create a separate environment for each project. That way one project's installed package versions do not have to change another project's versions.
- 02:51
The deeper website guide models two projects that require different versions of the same library. The dictionaries in that browser example are only an illustration; they do not install any packages. After creating the environment, you may activate it in your shell. In PowerShell, the shown command is dot venv backslash Scripts backslash Activate dot ps one. In a POSIX shell, the example is source dot venv slash bin slash activate. Activation changes how the shell resolves commands such as python and pip. It does not change the website's browser interpreter or automatically rewrite your source files.
- 03:31
If your shell's activation policy blocks the command, consult the official guide for that shell; you can also run the environment's Python executable by its path. After activation, run python minus m pip followed by two minus signs and version. The minus m form ties pip to the selected Python interpreter, helping you check where packages would be installed. Do not install a package just to see whether the environment works; first verify which pip you would be using. When finished, the shell command deactivate leaves the environment in that terminal. Now switch back to the website's browser console. There, import sys and inspect sys dot version info.
- 04:14
The prepared function returns a tuple containing the running interpreter's major and minor version. Print that pair as a readable version label. It describes the interpreter executing this page, not your computer's installation and not necessarily the newest Python release. The exact minor version can change as the site's runtime changes, so do not hard-code a particular number. The guide also shows how to compare a major and minor tuple with a minimum version without assuming the displayed minor release. It shows a Path object for a possible dot venv configuration file too. Constructing that path in the browser does not create the directory on your machine.
- 04:57
The distinction between a shell command, Python source code, and a path description is essential. Once you understand which interpreter runs which code, package setup problems become much easier to diagnose. A common mistake is typing py minus m venv dot venv into the site's Python editor. That is a shell command, not a Python statement, so the editor cannot use it to create an environment on your laptop. Run it in a local terminal from the intended project folder. A second mistake is checking a version in one terminal and later running a different command that selects another interpreter.
- 05:37
Check the command used for the actual project and verify pip through that interpreter. A third mistake is treating activation as if it installed Python globally or modified the browser runtime. Activation only affects the current shell's command selection. You can also work without activation by invoking the environment's interpreter directly. Finally, avoid hard-coding the site's exact minor version in the practical task. Read sys version info so the exercise stays correct when the browser runtime is updated. Complete runtime version in the website editor. Import sys, read the major and minor fields from sys version info, and return them as a two-item tuple.
- 06:22
Before running, predict that the first number is three in this course, but let the program tell you the exact second number. Compare its output with a local version command if you have Python installed; a difference is possible and does not mean either command is broken. Use the task checker to verify that the function reads the running interpreter rather than returning a fixed guess. Finish the three-question quiz. When you are ready to work locally, follow the official installation instructions, create a small project folder, make a virtual environment, activate it or invoke its interpreter by path, and verify pip before installing packages.
- 07:03
The website guide and source links are on the lesson page. The on-screen link opens the runnable browser task and the more detailed walkthroughs, while the local terminal remains the place for operating system setup.
Understand the concept
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.