This lesson is optional. Nothing else in the course needs it: every exercise runs on this site. Do it when you want a Python that can reach the network, read your real files, and install other people's libraries. There is nothing to run on the page; the commands below go in a terminal on your own computer.
You're done when a file you wrote prints Hello from my computer in your
terminal, inside a virtual environment. The checklist at the end walks you
there.
Opening a terminal
A terminal is a window where you type commands instead of clicking.
- Windows: press the Windows key, type
PowerShell, press Enter. - macOS: press Cmd-Space, type
Terminal, press Enter. - Linux: press Ctrl-Alt-T, or find Terminal in your applications.
The terminal always works in one folder. Make a folder for your practice and move into it:
mkdir python-practice
cd python-practicemkdir makes a folder; cd ("change directory") moves into it. Every command
below assumes you're in this folder. cd .. goes back up one level.
Getting Python
Check whether you already have it. macOS and most Linux distributions ship a Python, though sometimes an old one:
python3 --versionIf that prints Python 3.11 or newer, you are set. Otherwise:
- Windows — install from the Microsoft Store, or python.org. Tick "Add Python to PATH" in the installer.
- macOS —
brew install pythonif you use Homebrew, otherwise the python.org installer. - Linux —
sudo apt install python3 python3-venv python3-pipon Debian/Ubuntu.
python or python3?
On Windows the command is usually python. On macOS and Linux, python3 is
the safe one — plain python may be missing or point at an ancient Python 2.
Running a file
Open a text editor (see An editor below, or Notepad/TextEdit for
now), type one line, and save it as hello.py inside python-practice:
print("Hello from my computer")Then run it from the terminal:
python3 hello.pyOn Windows, type python hello.py instead.
can't open file 'hello.py' means the terminal is in a different folder than
the file: check with ls (macOS/Linux) or dir (Windows), and cd to the
folder that holds it.
Running python3 with no file gives you the REPL — an interactive prompt
where each line runs as you press Enter. It is the closest thing to the
playgrounds on this site, and it is where experienced Python programmers test
small ideas. exit() or Ctrl-D leaves it.
Virtual environments
A virtual environment is a private folder of packages belonging to one project.
Without one, every pip install goes into your system Python, and two projects
that need different versions of the same library cannot both work.
Make one per project, in the project folder:
python3 -m venv .venvThen activate it:
source .venv/bin/activateOn Windows PowerShell, that line is .venv\Scripts\Activate.ps1 instead. The
first time, PowerShell may refuse with running scripts is disabled on this
system. Allow scripts you created yourself, for your account only, with:
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedAnswer Y, then run the activate line again. Your prompt gains a (.venv)
prefix, and python now means this project's Python. deactivate leaves
it.
Don't copy or share .venv
The folder is large, belongs to one machine, and rebuilds in seconds. Share
your code and a requirements.txt (below), never the .venv folder. If you
use git for version control, list .venv/ in a file named .gitignore so it
is never saved.
Installing packages
With the environment active, pip installs from the Python Package Index:
pip install requestsRecord what a project needs so someone else — or you, on another machine — can reproduce it:
pip freeze > requirements.txtAnd install from that record:
pip install -r requirements.txtThree packages worth knowing about early:
| Package | What it does |
|---|---|
requests | HTTP the pleasant way |
pytest | The testing tool almost everyone uses (Day 7) |
ruff | Fast linter and formatter — catches mistakes before you run |
An editor
Any text editor works, but a real Python editor will show you errors as you type. VS Code with the Python extension is the common choice and free; PyCharm is the heavyweight alternative.
Point your editor at the interpreter inside .venv so it sees the same
packages your program will.
A sensible project layout
myproject/
├── .venv/ # virtual environment, not committed
├── .gitignore
├── requirements.txt
├── README.md
├── src/
│ └── myproject/
│ ├── __init__.py
│ └── main.py
└── tests/
└── test_main.pyYou do not need all of this for a fifty-line script — but when a script grows into a program, this is the shape it grows into. Day 7 builds a small version.
Checklist
Tick these off in order. If one fails, the step above it tells you why.
python3 --version(orpython --versionon Windows) prints 3.11 or newer.cd python-practiceworks, andhello.pyis listed bylsordir.python3 hello.py(orpython hello.pyon Windows) printsHello from my computer.- After
python3 -m venv .venvand the activate line, your prompt starts with(.venv). - With
(.venv)showing,python hello.pystill prints the greeting.
What you learned
python3 --versionchecks what you have;python3 file.pyruns a program.python3alone opens the REPL for quick experiments.python3 -m venv .venvplussource .venv/bin/activateisolates a project.pip installadds packages;pip freeze > requirements.txtrecords them.- Keep
.venv/out of version control.