Learn
07.1 | TERMINAL COMMANDS AND VIRTUAL ENVIRONMENTS
01 | ANOTHER WAY TO TALK TO YOUR COMPUTER
Until now, you may have clicked a Run button. A terminal gives you a text-based way to ask the computer to perform tasks. The terminal is the window; the shell is the program interpreting commands inside it. PowerShell, Command Prompt, bash and zsh are shells, not Python.
Think of a project as a workshop. Before running a tool, you need to know which workshop you are standing in, which file you want and which tool will use it. The current working directory is the folder against which relative paths are normally resolved.
A prompt such as PS C:\Users\Maya> or $ is displayed by the shell. Do not copy the prompt itself. If you see >>>, you are inside Python’s interactive interpreter. Use exit() to leave that interpreter before entering terminal commands.
02 | FIND YOUR WAY AROUND
Start in a folder you own for this course. List its contents before changing anything. cd means change directory, and .. means the parent directory. Quotation marks keep a folder name containing spaces together as one argument.
A relative path such as app.py starts from your current folder. An absolute path describes a location from the filesystem root. The exact root syntax differs between Windows and macOS or Linux.
The table uses PowerShell on Windows. Command Prompt uses cd to show its current directory and dir to list files. Do not assume every shell shares the same commands or path separators.
| Purpose | Windows PowerShell | macOS / Linux shell |
|---|---|---|
| Show current folder | Get-Location | pwd |
| List files | Get-ChildItem | ls |
| Create a folder | mkdir "task planner" | mkdir "task planner" |
| Enter it | cd "task planner" | cd "task planner" |
| Go to parent | cd .. | cd .. |
03 | RUN A FILE FROM ITS FOLDER
Save the following code as hello.py. In the terminal, enter its folder and run the interpreter with that filename as an argument. Saving and running are different actions; a terminal executes the saved file.
On many Windows installations the py launcher selects an installed Python. On macOS or Linux, python3 is commonly used. Availability depends on installation: verify the command rather than repeatedly changing your source when the shell cannot find Python.
The course examples require Python 3.10 or later. --version reports the selected interpreter version. A module is Python code that can be executed or imported; -m asks the interpreter to run a named module.
print("Hello from my project folder")EXPECTED OUTPUT
Hello from my project folder
04 | WHAT A VIRTUAL ENVIRONMENT SOLVES
Imagine one project needs a package version that another project cannot use. Installing everything into one shared Python makes those requirements collide. A virtual environment gives a project its own installation area for Python packages and interpreter entry points.
venv is a standard-library module that creates this environment. .venv is a conventional folder name, not a magic requirement. Keep it inside the project and exclude it from Git. Create it with the intended base interpreter.
An environment is package isolation, not a security sandbox. Code can still access files and networks with your account’s permissions. It also does not turn source code into a standalone executable for someone without Python.
TERMINAL | WINDOWS POWERSHELL
py --version py -m venv .venv
TERMINAL | MACOS OR LINUX
python3 --version python3 -m venv .venv
05 | ACTIVATE THE ENVIRONMENT
Activation adjusts the current shell’s search path so python normally refers to the environment interpreter. It may also add (.venv) to the prompt. Activation affects that shell session, not every terminal on the computer.
In PowerShell use the Scripts activation file below. In Command Prompt the equivalent is .venv\Scripts\activate.bat. In bash or zsh, source loads the activation script into the existing shell.
If a school-managed PowerShell blocks scripts, follow its support process or use the environment interpreter directly. Activation is convenient but optional; it is unnecessary to weaken a school-wide setting to complete this lesson.
TERMINAL | WINDOWS POWERSHELL
.venv\Scripts\Activate.ps1 python hello.py
TERMINAL | MACOS OR LINUX
source .venv/bin/activate python hello.py
06 | PROVE WHICH PYTHON IS RUNNING
A changed prompt is a useful clue, but inspect the interpreter itself. sys.executable reports its executable path. Within a normal venv, sys.prefix differs from sys.base_prefix.
Run interpreter_check.py from your project. Expect the executable path to include your .venv folder and the final line to be True. The actual path and version vary between machines.
In VS Code, select the .venv interpreter using Python: Select Interpreter. For IDLE, launch python -m idlelib using the environment interpreter, where Tk/IDLE is installed. Selecting an editor interpreter and activating a terminal are related actions, but do not assume one always changes the other.
import sys
print("Executable:", sys.executable)
print("Version:", sys.version.split()[0])
print("Inside a virtual environment:", sys.prefix != sys.base_prefix)07 | USE AN EXPLICIT INTERPRETER PATH
When activation is unavailable or you are troubleshooting, supply the interpreter path yourself. These commands execute the project’s Python without relying on a shell’s current search path.
Keep command lines out of .py files. The shell understands executable paths; Python source instead contains statements such as import sys. A SyntaxError after pasting python hello.py into a file is a context mistake, not a broken installation.
TERMINAL | WINDOWS POWERSHELL
.venv\Scripts\python.exe hello.py .venv\Scripts\python.exe -m pip --version
TERMINAL | MACOS OR LINUX
./.venv/bin/python hello.py ./.venv/bin/python -m pip --version
08 | FOLLOW THE INTERPRETER CHOICE
This fixed model illustrates command resolution. It does not create an environment on your device. Compare a default shell, an activated shell and an explicit executable path.
09 | LEAVE AND RECREATE
deactivate restores the shell settings that activation changed. Your source files and environment folder remain. A newly opened terminal may need activation again.
Environments are disposable build products. Move source and requirements between machines, then create a fresh environment there. Copying an existing .venv is unreliable because it contains machine-specific paths and depends on the original Python installation.
If recreating, first confirm the exact environment folder and close processes using it. Preserve source and data; recreate only the environment you intend. The next lesson records package requirements so a fresh environment can install them.
TERMINAL | AFTER ACTIVATION
python -m pip --version deactivate
10 | DIAGNOSE THE RIGHT PROBLEM
“File not found” commonly means the working directory or filename is wrong. “Command not found” means the shell cannot locate the executable. ModuleNotFoundError can mean the package is absent from the interpreter actually running your program.
Write down the folder, sys.executable and package command output before changing settings. This separates project navigation, Python selection and package installation into questions you can answer.
Try it: reopen your terminal, run the check through an explicit .venv path, and explain why that still works without activation.
PUT YOUR SKILLS TO WORK
Use the Section 7 challenges to practise these ideas independently. Work in your Python editor, predict results and keep a short test record.
Section 7 challenges Python course navigation