2  Executing Code

There are several ways to execute (also called “run”) Python code.

2.1 Executing files

When working with uv we can do this (inside of the working project directory):

uv run your-file.py

Or this:

uv run python your-file.py

Alternatively, we could manually activate the virtual environment and run the script:

source .venv/bin/activate
python your-file.py
Tip

Prefer uv run in order to let uv take care of several useful things for us under the hood, such as syncing our dependencies.

2.2 Built-in REPL

As per Wikipedia:

A read–eval–print loop (REPL) is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user.

If we type uv run python in our terminal and hit enter, we get into the built-in python REPL.

The built-in python REPL is fine, but as of today (October 2024) it has some limitations.

Note

Python core developers are putting a lot of effort into improving the REPL and Python 3.13 has already a number of great improvements, give it a try!

2.3 IPython REPL

An alternative to that is the IPython REPL. IPython is a whole different open source project that builds on top of python itself, but it is independent, thus we need to install IPython.

uv add ipython
Tip

Take a look at the content of pyproject.toml to see how uv added ipython as a project dependency. We’ll take a closer look at that later.

We can fire up the IPython REPL:

uv run ipython

The IPython REPL brings some nice features that make this interactive coding environment much powerful and handy to interact with, such as magic commands. These are some commands I recommend checking out:

  • ?
  • ! <COMMAND>: Call the system COMMAND, eg !ls to list contents of current directory.
  • %run: Run a file inside IPython as a program.
  • %time: Time execution of a Python statement or expression.
  • %timeit: Time execution of a Python statement or expression.

2.3.1 Exercises

Get familiar with the REPL:

  1. Add two or more numbers (try with and without decimals)
  2. Multiply two or more numbers
  3. Multiply 3 twenty two times
  4. Print your name using the print function
  5. Add two strings, e.g. “hello” + “world”
  6. Create the file src/pycourse/main.py that prints your name and run it inside the IPython REPL
  7. Same but print it’s execution time

2.4 Jupyter Notebook

Notebooks run on the browser and are a way to mix code with other kinds of data, such as images, videos or widgets. They are a very useful tool for explorative analysis. If you ever used something like Mathematica you’ll be already familiar with them.

There are several versions of notebooks, we are going to use JupyterLab. Let’s install it:

uv add jupyterlab

Let’s create a directory for notebooks in our project:

mkdir noteboooks

So far your project should look like this:

Launch a jupyterlab session:

uv run jupyter lab
Tip

Some people have strong opinions on editors and coding workflows. Don’t waste time getting into that, just use whatever editor/workflow makes you productive.