Different ways to use Python#

We present here different ways to run Python programs, from the most accessible to the most flexible.

In the first part of this training, we used JupyterLite to run Python directly in the browser — no installation required. This is convenient for learning, but a Python developer also needs to be able to run Python locally, understand which interpreter is active, and work efficiently from the terminal or an IDE.

This section walks through the main approaches: running scripts from the terminal, working interactively with IPython, editing code in an IDE, and using JupyterLab for notebook-style workflows.

Hide code cell content

import os

os.chdir("../common/examples")

Python: a language and some interpreters#

Python is a programming language.

The most common way to execute Python code is to interpret it. Through misuse of language, one says that Python is an interpreted language (like Bash, Matlab, and in contrast with Fortran, C or C++). To interpret code, we need an interpreter, i.e. a program parsing Python code and running computer instructions.

The interpreter is a program, which is somewhere is your computer. The program is associated with a command (typically python or python3) that one can use in the terminal without giving the full path.

Most of the time, users of Python software don’t need to know where is the interpreter. However, as a Python developer, it is usually good to have an idea about which interpreter is used.

Exercise 6

Open a terminal. Depending on your OS, run the following commands:

Get-Command python
Get-Command py
which python
which python3

The results depend on your computer state…

On Linux, you should have the system Python (a command python3 corresponding to a file /usr/bin/python3). On Linux, when no virtual environment is activated, there is usually no python command.

Execute a script with the command python3#

Exercise 7

Run a script with python3 (helloworld.py is just a text file):

python3 src/common/examples/helloworld.py

From the terminal, change the current directory with cd src/common/examples and rerun the file with just

python3 helloworld.py

Open the file with an editor (for example VSCode that can be launched from the terminal with code .), change the name in the code and rerun the file from the terminal.

Work interactively with IPython#

The command ipython launches the program IPython, which is used for interactive python.

Exercise 8

  • In IPython, you can execute a first interactive instruction:

2 + 2
4
3 / 4
0.75
  • Run the script from IPython.

%run helloworld.py
Hello world
My name is Pierre

In the file helloworld.py, a variable called name is defined.

print(name)
Pierre
  • Help on an object can be displayed with the question mark (try it):

name?
  • Try to type name. and to press on tab… The tab key is used to tell you how what you typed can be completed.

  • Try to use the top and bottom arrows…

Python in an IDE (Visual Studio Code)#

Launch the application Visual Studio Code, a lightweight IDE (Integrated Development Environment).

VSCode is first a very good code editor with:

  • syntax coloring,

  • code analysis powered by pyflakes and pylint,

  • introspection capabilities such as code completion.

For this training we will mostly use this feature, but VSCode is actually much more than that.

Important

It is very important to use a good editor to code in Python (for example Spyder, emacs or vi (with a good setup!), or Visual Studio Code).

Exercise 9 (Install VSCode extensions for Python)

Open the directory of this training with VSCode:

cd py-edu-fr
code .

Install the following extensions via the Extensions panel (Ctrl+Shift+X / Cmd+Shift+X):

  • Python (look for ms-python) — language support, environment management, debugger

  • Ruff (charliermarsh) — fast linter and formatter

Exercise 10 (Get familiar with VSCode)

Open the file src/common/examples/helloworld.py.

  • Notice the syntax coloring and the Problems panel, where Ruff signals errors and style issues in real time, before you even run the code.

  • Check the status bar at the bottom: it shows the active Python interpreter. Click on it to switch environments. This interpreter is the same one used when you type python3 in the terminal, so it should match your virtual environment.

  • Modify the file, then switch to your external terminal (Alt+Tab) and rerun it in IPython with the up arrow. This edit–run loop is very efficient.

Python in the browser (JupyterLab)#

Jupyter is a very powerful tool to present results (see these examples). For example, the presentations of this python training are made with Jupyter Notebooks.

Let us launch Jupyter Lab with the command:

jupyter-lab

This should start a server and maybe also open the corresponding application in your browser. If the application does not automatically starts, use one of the http addresses given in the terminal.

Exercise 11 (Jupyter Lab interface)

In the application, navigate in src/en/pure-python-basic and click on strings.md. Jupyter opens it as a text file. Now, right click on the same file and Open With -> Notebook. Now we see that it’s a notebook.

Get familiar with the UI. Learn how to run one cell and how to “restart kernel and run all cells”.

Note

Jupyter is a web application. It works in a server-client mode. The command jupyter-lab opens a server which can be used locally in your browser. One can also use a distant Jupyter server running on another computer.

The code of the notebooks runs on the machine hosting the server.

JupyterLite

JupyterLite is a JupyterLab distribution that runs entirely in the browser.

You can use the JupyterLite of the training. In this case, the code of the notebook runs on your computer (but through your web browser).

Pyodide and PyScript

Pyodide is a port of CPython to WebAssembly, making it possible to run Python — and a large part of the scientific Python stack — entirely in the browser, without a server. It is the engine behind JupyterLite.

PyScript builds on Pyodide (and other runtimes such as MicroPython) to let you embed Python code directly in HTML pages, with access to the browser’s DOM and JavaScript APIs. It is aimed at building interactive web applications powered by Python.

Both technologies are still maturing, but they represent a significant shift: Python no longer requires a server or a local installation to run in a web context.