Why this matters
You already automate. A line of shell that loops over hosts, a cron entry that rotates a log, a grep that finds the failed logins. Shell is excellent at gluing commands together, and nothing in this course asks you to give it up. It becomes painful at a predictable point: the moment your automation has to *understand* data rather than pass it along. Parsing a JSON response from an API, comparing two inventories to find what is missing, retrying an HTTP call with a back-off, writing a report that another program can read. In shell those jobs turn into fragile pipelines of awk, sed and jq that only their author can maintain.
Python is the tool that most operations teams reach for at that point. It is installed on every Ubuntu Server 24.04 machine as python3, its standard library covers files, JSON, CSV, HTTP, processes and logging without installing anything, and code written in it can be tested before it touches production. The whole of this course builds toward one practical outcome: a scheduled tool that reads an inventory, checks a server and writes a report. Today you get the foundations in place: the interpreter, the interactive prompt, script files, and an isolated project environment.
Everything below was run on an Ubuntu 24.04 machine named linux01 as a normal user called student. Your username will differ; where you see /home/student, read your own home directory.
Concepts
The interpreter is the program that reads Python code and executes it. On Ubuntu 24.04 it is python3, version 3.12. There is deliberately no bare python command on a fresh server; you will get one inside a virtual environment shortly.
The REPL (read–eval–print loop) is what you get when you run python3 with no arguments. It shows a >>> prompt, evaluates each line you type, and prints the result of any expression. It is the fastest way to try an idea or check what a function returns. Leave it with exit() or <kbd>Ctrl</kbd>+<kbd>D</kbd>.
A script is a text file of Python code, conventionally ending in .py, run with python3 file.py. Add a first line #!/usr/bin/env python3 (a *shebang*) and make the file executable with chmod +x, and it runs like any other command. #!/usr/bin/env python3 asks the environment for whichever python3 is first on the PATH, which is exactly what you want once virtual environments are involved.
A virtual environment (venv) is a private copy of the interpreter's package directory, created inside your project. Packages you install with pip land in the venv, never in the operating system's Python. This matters more on Ubuntu 24.04 than on older releases: the system interpreter is marked *externally managed* (PEP 668), and pip refuses to install into it at all. That refusal is protecting the tools that Ubuntu itself runs with Python. The rule is simple: one project, one venv, and never sudo pip.
Activation is a shell trick. source .venv/bin/activate puts the venv's bin directory first on your PATH, so python and pip now mean the venv's copies, and your prompt gains a (.venv) prefix. deactivate undoes it. Nothing is installed or removed by activating; it only changes which interpreter your shell finds first.
pip installs packages from the Python Package Index (PyPI). Run it as python -m pip ... rather than bare pip: the -m form guarantees that pip belongs to the interpreter you think it does.
Project layout. Every lesson adds to the same directory, ~/pyops. It holds the venv in .venv/, one-file scripts in scripts/, later a package and tests. The venv is disposable and must never be copied between machines or committed to git; if it ever looks broken, delete it and recreate it in seconds.
*Other platforms.* On macOS the commands are identical once python3 is installed (Xcode command-line tools or Homebrew). On Windows use py -m venv .venv and .venv\Scripts\activate; paths use backslashes. The canonical path for this course is Ubuntu 24.04, and the checks run there.
Guided exercise
- Confirm which interpreter you have. Type the command and compare the version with the output below.
python3 --version
which python3Python 3.12.3
/usr/bin/python3- Start the REPL, evaluate a few expressions, and leave it. Notice that an expression on its own is echoed back, while
print()writes the value without quotes.7 / 2always gives a decimal;7 // 2rounds down to a whole number.
python3Python 3.12.3 (main, Aug 31 2026, 10:18:26) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 2
4
>>> 7 / 2
3.5
>>> 7 // 2
3
>>> "web-01".upper()
'WEB-01'
>>> import platform
>>> platform.python_version()
'3.12.3'
>>> hostname = "web-01"
>>> hostname
'web-01'
>>> print(hostname)
web-01
>>> exit()- Create the project directory and your first script. Use
nanoorvimto save the file exactly as shown; the first line is the shebang and the second is a *docstring*, a short description that tools can read later.
mkdir -p ~/pyops/scripts
cd ~/pyops
nano scripts/hello.py#!/usr/bin/env python3
"""Print a greeting with this machine's name and Python version."""
import platform
import socket
print(f"Hello from {socket.gethostname()} running Python {platform.python_version()}")- Run it two ways: through the interpreter, then directly after making it executable. The
f"..."string with values in braces is an f-string; lesson 2 covers it properly.
python3 scripts/hello.py
chmod +x scripts/hello.py
./scripts/hello.pyHello from linux01 running Python 3.12.3
Hello from linux01 running Python 3.12.3- See what Ubuntu says when you try to install a package outside a venv. Read the whole message; it is unusually helpful.
python3 -m pip install requestserror: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
...
hint: See PEP 668 for the detailed specification.- Create the virtual environment inside the project, activate it, and prove that
pythonnow points into it. A fresh venv contains only pip.
python3 -m venv .venv
source .venv/bin/activate
which python
python --version
python -m pip --version
python -m pip list/home/student/pyops/.venv/bin/python
Python 3.12.3
pip 24.0 from /home/student/pyops/.venv/lib/python3.12/site-packages/pip (python 3.12)
Package Version
------- -------
pip 24.0- Leave the venv and confirm that
pythondisappears again whilepython3remains. Then add the two housekeeping files every project should have..gitignorekeeps the venv and caches out of version control (the Git course covers the rest).
deactivate
which python
which python3
printf '.venv/\n__pycache__/\n*.pyc\n.env\n' > .gitignore
printf '# pyops\n\nPractice project for the Python Programming and IT Automation course.\n' > README.md
ls -la/usr/bin/python3
total 24
drwxr-xr-x 4 student student 4096 Sep 13 05:22 .
drwxr-x--- 3 student student 4096 Sep 13 05:21 ..
-rw-r--r-- 1 student student 31 Sep 13 05:22 .gitignore
drwxr-xr-x 5 student student 4096 Sep 13 05:21 .venv
-rw-r--r-- 1 student student 194 Sep 13 05:22 README.md
drwxr-xr-x 2 student student 4096 Sep 13 05:21 scriptsThe first which python prints nothing, because outside the venv there is no python command. That is normal on Ubuntu.
Troubleshooting
`python3 -m venv .venv` fails with "ensurepip is not available" → the venv module is packaged separately on Ubuntu → sudo apt install python3-venv, delete the half-made .venv directory, and run the command again. The exact message we observed on a machine without the package ends with Failing command: /tmp/demo-venv/bin/python3 and tells you which package to install.
`error: externally-managed-environment` when you expected the install to work → your venv is not active in this shell → run which python; if it prints /usr/bin/python3 or nothing, run source .venv/bin/activate from the project directory and try again.
The prompt never shows `(.venv)` → the activate script was run in a sub-shell (sh .venv/bin/activate or bash .venv/bin/activate) so its PATH change vanished when that shell exited → use source (or .), which runs it in your current shell.
`./scripts/hello.py: Permission denied` → the execute bit is missing → chmod +x scripts/hello.py, or run it as python3 scripts/hello.py, which never needs the bit.
`/usr/bin/env: 'python3\r': No such file or directory` → the file was edited on Windows and has carriage returns at the end of each line → recreate it on the server, or convert it with sed -i 's/\r$//' scripts/hello.py.
Check your understanding
- Why does Ubuntu 24.04 refuse
python3 -m pip install requests, and what is the supported alternative? - What does
source .venv/bin/activatechange on your system, and what does it leave untouched? - A colleague copies your
.venvdirectory to another server and it does not work. Why is that expected?
Answers
1. The system interpreter is marked externally managed (PEP 668) so that pip cannot overwrite packages
that apt and Ubuntu's own tools depend on. Create a venv and install there.
2. Only your shell's PATH (and prompt) for the current session, so that python and pip resolve to the
venv's copies. It installs nothing and changes no files.
3. A venv records absolute paths to the interpreter that created it and is built for that machine's
Python; it is meant to be recreated, not copied. Recreate it with python3 -m venv .venv and reinstall
the pinned dependencies (lesson 7).
Summary and next step
- Ubuntu 24.04 ships Python 3.12 as
python3; the REPL is for trying ideas, scripts are for keeping them. - One project, one venv:
python3 -m venv .venv,source .venv/bin/activate,python -m pip install ...,
deactivate. Never sudo pip, and never commit .venv/.
~/pyopsnow hasscripts/hello.py, a venv, a.gitignoreand a README; every later lesson adds to it.
Next, in *Values, types, strings and f-strings*, you take apart real operational text (log lines, host names, byte counts) with Python's core types.
References
- The Python Tutorial, "Using the Python Interpreter" — https://docs.python.org/3/tutorial/interpreter.html (accessed 2026-09-13)
- venv — Creation of virtual environments — https://docs.python.org/3/library/venv.html (accessed 2026-09-13)
- Installing packages using pip and virtual environments (Python Packaging User Guide) — https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/ (accessed 2026-09-13)
- PEP 668 – Marking Python base environments as "externally managed" — https://peps.python.org/pep-0668/ (accessed 2026-09-13)
- Ubuntu Server documentation — https://documentation.ubuntu.com/server/ (accessed 2026-09-13)
Sign in to record your progress
Signing in saves eligible progress. It does not enroll you or include a lab; review the career path for access terms.