LEARN PYTHON | Terminal Commands And Virtual Environments

Lesson objective

Navigate a project folder, run a chosen Python interpreter and create, activate, inspect and recreate a project-specific virtual environment.

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.

PurposeWindows PowerShellmacOS / Linux shell
Show current folderGet-Locationpwd
List filesGet-ChildItemls
Create a foldermkdir "task planner"mkdir "task planner"
Enter itcd "task planner"cd "task planner"
Go to parentcd ..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

MATCH THE PYTHON TERM TO ITS MEANING

Terminology

Terminology

Terminal

A text interface containing a shell session.

Shell

A program interpreting operating-system commands.

Working directory

The current folder used to resolve ordinary relative paths.

Relative path

A path interpreted from a starting location.

Absolute path

A path giving a location from the filesystem root.

Virtual environment

A project-specific Python environment with a separate package installation area.

venv

The standard-library module used to create virtual environments.

Activation

Adjusting the current shell so environment commands are found first.

Interpreter

The program executing Python instructions.

Launcher

A tool selecting and starting an installed interpreter.

Module

Python code that can be imported or executed by module name.

-m

The interpreter option for executing a named module.

sys.executable

The path of the currently running Python executable.

sys.prefix

The location prefix for the running Python installation or environment.

sys.base_prefix

The prefix of the base Python used by a normal venv.

deactivate

A shell function restoring settings changed by activation.

Search path

The locations a shell searches for executable commands.

Recreation

Building a fresh environment from recorded requirements.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Where do you enter python hello.py?
2. Which command changes folder?
3. What does activation normally change?
4. Which checks identify the running interpreter?
5. Is activation required for an explicit .venv interpreter path?
6. Which is true of a virtual environment?
7. What does -m venv do?
8. What commonly explains a missing file?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Explain terminal.

2. Explain shell.

3. Explain working directory.

4. Explain relative path.

5. Explain absolute path.

6. Explain virtual environment.

7. Describe a useful test for this lesson.

8. Evaluate one limitation of the demonstrated approach.

CODE IT | YOUR INDEPENDENT CHALLENGE

Create a fresh environment and prove which interpreter runs a saved file. Explain a wrong-directory error.

Work in your Python editor. Keep a working version, explain one design choice and record at least two tests. Use the Learn tab for worked examples before comparing your result.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for Terminal Commands And Virtual Environments is coming soon. Complete the matching task and written questions in your exercise book.