Python: Programming, Automation and Data Analysis
Exit

Lesson 1 of 14 · Language basics

Python: installation, interpreter, first script, venv and pip

45 min Python 3.12 (Debian, git, pip)

Objective

Set up a clean Python workstation: know which Python is installed, talk to the interpreter, write and run a first script, then give each project its own virtual environment so you can install libraries with pip without breaking the system.

Key points

  • Python is an interpreted language: there is no compilation step; you write a .py file and run it with python3 fichier.py.
  • The interactive interpreter (python3 on its own, >>> prompt) lets you try an idea in a few seconds.
  • On Linux, use python3; on Windows, the py launcher. Always check the version with python3 --version.
  • A virtual environment (python3 -m venv .venv) is a folder that contains a Python and packages specific to one project.
  • You install packages with python -m pip install … inside the activated environment, never with sudo pip.

1. Why Python?

Python is popular because it reads almost like simple English and comes with a very rich standard library (files, dates, JSON, CSV, HTTP, compression, SQLite databases…). The same language is used by:

  • the administrator who replaces a 300-line shell script with 60 readable lines;
  • the developer who writes a web API for a delivery application in Casablanca;
  • the analyst who turns a cooperative's monthly Excel sales export into a clear report.

The language is named after the British comedians Monty Python, not after the snake. The official tutorial makes constant references to them.

2. Installing Python

On Linux

Most distributions already ship Python 3: many system tools depend on it. Check:

python3 --version          # e.g. Python 3.12.3
which python3              # /usr/bin/python3

If it is missing, install it with your distribution's package manager, together with the virtual environment module:

# Debian / Ubuntu
sudo apt install python3 python3-venv python3-pip

# Rocky / Alma / RHEL
sudo dnf install python3 python3-pip

Never remove the system Python, and do not replace /usr/bin/python3 with another version: dnf, apt and tools such as firewalld rely on it.

On Windows

Install Python from the official website (python.org) or the Microsoft Store. The command to remember is the py launcher:

py --version
py -3.12 script.py         # choose a specific version if several are installed

When you install from python.org, tick the option that adds Python to the PATH.

Which version?

Use a recent, maintained 3.x version (3.12 in our labs). Python 2 has been dead since 2020: if an old tutorial writes print "bonjour" without parentheses, it dates from that era.

3. The interactive interpreter

Type python3 without arguments:

$ python3
Python 3.12.3 (main, …) [GCC …] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 + 3
5
>>> 1500 * 1.20          # the price incl. VAT of an item at 1,500 DH excl. VAT
1800.0
>>> "Rabat".upper()
'RABAT'
>>> help(len)            # the built-in help; press q to exit
  • >>> is the primary prompt; ... appears when a statement continues over several lines (after an if or a for);
  • in interactive mode, the value of an expression is displayed automatically; in a script, you need print();
  • to exit: Ctrl-D on Linux, Ctrl-Z then Enter on Windows, or quit().

Two other ways to start the interpreter are very useful in system administration:

python3 -c 'print(19 * 1.2)'        # run a short statement
python3 -m http.server 8000         # run a standard library module as a program
python3 -i script.py                # run the script, then stay in interactive mode

4. The first script

A script is a simple UTF-8 text file with the .py extension. Create facture.py:

# facture.py — calculating a simple invoice
print("Coopérative Tifaout — facture")
prix_ht = 250
print("Prix TTC :", prix_ht * 1.20, "DH")

Run it:

python3 facture.py
# Coopérative Tifaout — facture
# Prix TTC : 300.0 DH

Notes:

  • # starts a comment, which Python ignores;
  • Python reads the file from top to bottom and runs each line in order;
  • indentation matters: a line must not start with spaces for no reason (you will see why with conditions). Set your editor to 4 spaces, and never mix in tabs.

Receiving arguments

The sys module gives access to the command-line arguments:

# salut.py
import sys
print("Bonjour", sys.argv[1])
python3 salut.py Youssef      # Bonjour Youssef

sys.argv[0] is the name of the script, sys.argv[1] the first argument, and so on (lesson 3.1: the argparse module does this properly).

5. Making a script executable

On Linux, you can run a script like a command, without typing python3 in front of it. You need two things:

  1. a first shebang line that specifies the interpreter;
  2. the execute permission.
#!/usr/bin/env python3
print("Sauvegarde du serveur web…")
chmod +x sauvegarde.py
./sauvegarde.py

#!/usr/bin/env python3 looks for python3 in the PATH: it is more portable than #!/usr/bin/python3, and it respects the activated virtual environment. Be careful: the file must have Unix line endings (LF). A file saved on Windows (CRLF) causes the error /usr/bin/env: 'python3\r': No such file or directory.

6. Virtual environments

The problem

A riad's booking website uses version 1 of a library; the invoicing tool, on the same server, requires version 2. With a single Python installation, one of them breaks. And installing packages into the system Python can break distribution tools.

The solution: one folder per project

cd ~/projets/facturation
python3 -m venv .venv            # creates the .venv folder
source .venv/bin/activate        # Linux / macOS
# .venv\Scripts\activate         # Windows
(.venv) $ which python
/home/sara/projets/facturation/.venv/bin/python
(.venv) $ deactivate             # return to the system Python
  • venv copies (or links) the interpreter that you ran it with: python3.12 -m venv .venv gives a 3.12 environment;
  • the name .venv is the convention: hidden, explicit, and it cannot be confused with a .env configuration file;
  • activating an environment only changes the current shell (the PATH and the prompt);
  • without activating it, you can call .venv/bin/python script.py directly: this is what you do in a cron job or a systemd unit;
  • the .venv folder is not copied from one machine to another and never goes into Git (add it to .gitignore).

To find out, from Python, whether you are in a virtual environment:

import sys
print(sys.prefix)          # the environment folder
print(sys.base_prefix)     # the original Python; different from sys.prefix in a venv

7. Installing packages with pip

pip downloads packages from PyPI (the Python Package Index). Use the python -m pip form: you are sure to use the pip of the active Python.

(.venv) $ python -m pip install requests              # latest version
(.venv) $ python -m pip install "requests==2.32.3"    # specific version
(.venv) $ python -m pip install --upgrade requests    # upgrade
(.venv) $ python -m pip list                          # installed packages
(.venv) $ python -m pip show requests                 # details of a package
(.venv) $ python -m pip uninstall requests

requirements.txt

So that a colleague (or the production server) gets exactly the same versions:

(.venv) $ python -m pip freeze > requirements.txt     # pin the versions
(.venv) $ cat requirements.txt
certifi==2024.8.30
requests==2.32.3
…
# on the other machine:
(.venv) $ python -m pip install -r requirements.txt

requirements.txt goes into Git; the .venv folder does not.

The "externally-managed-environment" error

On Debian 12, Ubuntu 23.04 and later, pip install outside a virtual environment is refused with the message externally-managed-environment. This is not a bug: the distribution protects its Python. The right answer is to create a venv, not to add --break-system-packages.

8. An editor

Any text editor will do, but an editor that understands Python will save you a lot of time: syntax highlighting, automatic 4-space indentation, error detection. VS Code (with the Python extension) and PyCharm Community are free. On a server, vim or nano are enough for small scripts.

Common pitfalls

  • python versus python3: on many Linux systems, python does not exist or refers to something else. Write python3 (or python only in an activated venv).
  • Installing with sudo pip install: you modify the system Python and risk breaking tools. Always use a venv.
  • Forgetting to activate the venv: the package is installed somewhere else and import requests fails with ModuleNotFoundError. Check with which python.
  • Mixing tabs and spaces: TabError or IndentationError. 4 spaces, everywhere.

Lab

Your lab runs on a machine with Python 3.12 and its standard library, without Internet access (so no pip install of a remote package). You will:

  1. write /root/bonjour.py, your first script;
  2. make it executable with a shebang and chmod +x;
  3. write /root/version.py, which reads the version through sys.version_info;
  4. create the virtual environment /root/projet/.venv;
  5. prove that you are really using the pip of this environment.

To write a file without an editor:

cat > /root/bonjour.py <<'EOF'
print("Marhaba, MasterLinux !")
EOF

Quiz

  1. Which command starts the interactive interpreter on Linux? And on Windows?
  2. What does the line #!/usr/bin/env python3 at the top of a script do?
  3. Why create one virtual environment per project?
  4. Why write python -m pip install rather than pip install?
  5. Which file lets you reinstall exactly the same package versions on another server, and which folder must never go into Git?
Answers
  1. python3 on Linux; py (the launcher) on Windows.
  2. It is the shebang: when the script is run directly (./script.py, with the execute permission), the system uses the python3 found in the PATH to run it.
  3. So that each project has its own library versions, without conflicts between projects and without touching the system Python.
  4. Because python -m pip is guaranteed to use the pip of the active interpreter (the venv's one), whereas the pip command may refer to another Python.
  5. requirements.txt (produced by python -m pip freeze); the .venv folder never goes into Git.

Reference sources: official Python tutorial in French (PSF license). Original MasterLinux text.

Practical exercises

To do on Python 3.12 (Debian, git, pip)

The online terminal is coming soon. Meanwhile, do these tasks on your own Linux machine, a virtual machine or WSL.

  1. Create the script /root/bonjour.py that displays exactly: Marhaba, MasterLinux !
    Show a hintOne line is enough: print("Marhaba, MasterLinux !"). Write it with echo or cat > /root/bonjour.py <<'EOF'.
    Show the solution
    printf '%s\n' 'print("Marhaba, MasterLinux !")' > /root/bonjour.py

    Check : test -f /root/bonjour.py && test "$(python3 /root/bonjour.py)" = "Marhaba, MasterLinux !"

  2. Make this script directly executable: the command /root/bonjour.py (without typing python3 in front of it) must display the same message. You need a shebang line and the execute permission.
    Show a hintFirst line: #!/usr/bin/env python3 — then chmod +x /root/bonjour.py.
    Show the solution
    printf '%s\n' '#!/usr/bin/env python3' 'print("Marhaba, MasterLinux !")' > /root/bonjour.py && chmod +x /root/bonjour.py

    Check : test -x /root/bonjour.py && head -n 1 /root/bonjour.py | grep -q "^#!.*python3" && test "$(/root/bonjour.py)" = "Marhaba, MasterLinux !"

  3. Create /root/version.py that displays the interpreter version in the form "Python 3.12" (major.minor only), by reading sys.version_info: the result must stay correct if the interpreter changes.
    Show a hintimport sys then print(f"Python {sys.version_info.major}.{sys.version_info.minor}") — do not hard-code 3.12.
    Show the solution
    printf '%s\n' 'import sys' 'print(f"Python {sys.version_info.major}.{sys.version_info.minor}")' > /root/version.py

    Check : test -f /root/version.py && ! grep -q "3\.12" /root/version.py && test "$(python3 /root/version.py)" = "$(python3 -c 'import sys; print("Python %d.%d" % sys.version_info[:2])')"

  4. Create a virtual environment in /root/projet/.venv (the /root/projet folder does not exist yet).
    Show a hintmkdir -p /root/projet then python3 -m venv /root/projet/.venv
    Show the solution
    mkdir -p /root/projet && python3 -m venv /root/projet/.venv

    Check : test -x /root/projet/.venv/bin/python && /root/projet/.venv/bin/python -c 'import sys; sys.exit(0 if sys.prefix != sys.base_prefix else 1)'

  5. Save in /root/projet/pip.txt the output of the "pip --version" command FROM THE VIRTUAL ENVIRONMENT (not the system one): it must show the path /root/projet/.venv.
    Show a hint/root/projet/.venv/bin/python -m pip --version > /root/projet/pip.txt (or activate the environment first with source).
    Show the solution
    /root/projet/.venv/bin/python -m pip --version > /root/projet/pip.txt

    Check : test -f /root/projet/pip.txt && grep -q "^pip " /root/projet/pip.txt && grep -q "/root/projet/.venv" /root/projet/pip.txt

Official references consulted: Mise en bouche — https://docs.python.org/fr/3/tutorial/appetite.html ; Mode d'emploi de l'interpréteur Python — https://docs.python.org/fr/3/tutorial/interpreter.html ; Environnements virtuels et paquets — https://docs.python.org/fr/3/tutorial/venv.html ; Annexe (mode interactif, scripts exécutables) — https://docs.python.org/fr/3/tutorial/appendix.html. Original MasterLinux text.