2  Executing Code

There are several ways to execute 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 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.
  • %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. Run the file hello.py inside the REPL
  7. Same but print it’s execution time

2.4 Code Editor

When editing code more extensively we also want to have a so called integrated development environment (IDE). There are many options out there. Do not spend too much time deciding which to pick, you can always change your choice later.
VS-Code is a reasonable general choice as of today, it is free and widely adopted. For a user experience more similar to matlab and oriented to scientific computing you can check out Spyder.

2.5 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

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.