How to run a Python program

CPython — the king of Pythons

  • Install it from python.org, or with your favourite package manager: apt install python / brew install python / …
  • The current stable release is Python 3.14; Python 3.15 is scheduled for 1 October
  • More often than not, when people say “Python”, they mean CPython
  • github.com/python/cpython is a great C codebase — check it out!
  • Run a program with python program.py
  • You can also run python -c "print('Hello Python <3')"

REPL

  • Read–Eval–Print Loop — but everyone just calls it a REPL
  • Just run python without arguments!
  • You don’t even need to explicitly print

IPython

  • An even better REPL!
  • It needs to be installed separately, but that is as easy as pip install ipython, brew install ipython, apt install ipython3, …

Jupyter

  • Like a REPL, but you can return to earlier cells and use the power of a GUI
  • Not for running programs, but great for experimenting with Python
  • Available locally and online — see also Google Colab
  • Notebooks can be saved in files and opened in an IDE

IDE

  • Even moderately large projects use multiple Python files, so an IDE is convenient for quick navigation and debugging
  • PyCharm logoPyCharm and VS Code logoVS Code are currently the most popular choices
  • IDEs operate with projects rather than with files: they can manage multiple run configurations, code navigation, deployment and source control integration, common settings, etc...

Interpreter vs Compiler

  • CPython is typically called an interpreter — why?
  • What’s the difference between an interpreter and a compiler?
  • Python’s virtual machine and bytecode
  • One can say that CPython compiles Python to bytecode… and that’s right!
  • CPython even has experimental JIT compilation, and PyPy has had it for years
  • Why, then, is Python slower than Java?

Managing dependencies

  • Usually, you want your code to use some third-party libraries
  • 99.9% of useful Python libraries are hosted on one website: pypi.org
  • You cannot just import another library, because you need to “install” it first
  • “Install” basically means downloading its source code and copying it into your Python installation’s folder, where import looks for it
  • Okay, it also runs python setup.py from this folder!
  • The standard Python way to install a library is pip install <name>
  • Fun fact: pip means “Pip Installs Packages”
  • But that’s not the only way — we’ll talk about it later!

Managing versions

  • What if one project needs django==3.2, while another needs django==4.2? How can you install both versions for the same Python?
  • Even worse: what if different projects require different Python versions?
  • You can have different Python environments and switch between them! They are called virtual environments, a built-in Python feature
  • A virtual environment is a folder with a copy of Python and all its dependencies at specific versions
  • Activate the environment before working in it: <venv>\Scripts\activate.bat on Windows or source <venv>/bin/activate on Linux/macOS
  • IDEs have full support for virtual environments too

Managing dependency versions

  • Let’s assume you work on a large project that uses multiple libraries
  • You’ve downloaded the code and now need to install all these libraries — pip install <what>?
  • Python has requirements.txt for that
    Django==4.2.11
    requests==2.32.3

Managing dependency versions

  • Note: modern Python uses pyproject.toml instead, but we’ll talk about it a bit later
    [project]
    dependencies = [
        "Django>=4.2,<5",
        "requests>=2.32",
    ]
  • With requirements.txt, install all dependencies with pip install -r requirements.txt — they will be installed into your active virtual environment

Modern approach

  • People used to use virtual environments and pip, but did not like the lack of features, the slowness, and so on
  • So new tools appeared: first poetry and pyenv, and now uv
  • uv combines environment management and package installation, and it supports pyproject.toml
    [project]
    name = "my-project"
    version = "0.1.0"
    description = "My project"
    requires-python = ">=3.12"
    dependencies = [
        "Django>=4.2,<5",
        "requests>=2.32",
    ]
  • Give it a try when you start a new project!

The most modern approach

  • Just open chatgpt.com — it has its own Python runner :-)

Shebang bonus topic

  • When you open a Python file that can be run as an entry point, you often see something like #!/usr/bin/env python — what does it mean?
  • It’s a shebang: on Linux and other Unix-like operating systems, it tells the OS how to run the file, so you can use ./program.py instead of python program.py. Unfortunately, it does not work on Windows
  • /usr/bin/python might not be the Python from your virtual environment, so we use /usr/bin/env python, which finds the active virtual environment’s Python