Astrophysics II : interactions radiation-matter

PHYS-472

Getting started with Python

This page is part of the content downloaded from Getting started with Python on Sunday, 25 January 2026, 03:21. Note that some content and any files larger than 50 MB are not downloaded.

Description

A short guide on what Python is, how to install it and how to manage virtual environments and packages

Page content

Getting started with Python

Some of the exercise hours in this course will involve hands-on coding sessions, to allow you to experiment with a galaxy evolution code. This page is a short guide on how to set up a Python environment for this purpose. If you are already familiar with Python, you may still find useful information on e.g. managing virtual environments, but should otherwise be good to go.

Useful keywords

The following is a quick glossary of terms that will come up regularly when discussing python programming. Each keyword will be described in more detail below. A short description of what Python is is given here.

  • Python: The name of the programming language. It also designates the Python interpreter, which is the interactive console that opens when you run the 'python' command in a terminal.
  • Package or Module: an additional component that extends the functionality of Python. For example, there are packages to make plots, optimize numerical computations, work with neural networks for machine learning, etc.
  • Virtual Environment: Self-contained installation of Python along with a set of packages. There are different tools to create and manage virtual environments, such as the built-in venv or the third-party anaconda.
  • IPython: An enhanced version of the standard Python interpreter. You have to install it as a package, and can then open it by running the 'ipython' command in your terminal.
  • Jupyter: A suite of tools to work with Python interactively.
  • Jupyter Notebook or simply Notebook: An interactive way of programming in python, where code is split into 'cells' that can be run step-by-step, and produce an output that is immediately visible. This is what we will use in the course.
  • Kernel: A component that is required to run Jupyter notebooks. Typically, you have one kernel for each virtual environment.
  • Script: The traditional way of writing a python program (i.e. without notebooks). A script is a single file that is executed in one go, and it may import objects and functions from other scripts.
  • Package manager: Tool to install additional packages. The built-in package manager of python is pip, and the one of anaconda is conda.

Finally, let's also mention the two most commonly-used Python packages that you'll encounter all the time:

  • NumPy: A widely-used package that optimizes numerical computations in python. Basically, anytime you have to work with arrays of numbers (lists, matrices, etc.), you'll want to use numpy for efficiency.
  • Matplotlib: The standard plotting library for Python. Allows you to make graphs, display images, do scatter plots and histogram, etc.

Setting up Python

We will give two approaches, one to run Python remotely, and another to install it locally on your computer. The former has the advantage of requiring almost no setup, to quickly get started with the exercises of this course, while the latter requires a bit more work but is more flexible and also runs offline, which will be useful if you start using Python more frequently in the future. No matter which option you use in the end, we highly recommend at least connecting once to the Noto service described in option 1, as it includes some useful documentation. We will also not be able to offer detailed support during the exercise sessions if you go for the local install option, but we can certainly answer a couple of questions and encourage you to try it out!

Option 1: Run Python remotely on EPFL's Noto infrastructure

EPFL provides a service that lets you connect to a virtual machine which already has Python and a number of other useful tools installed, to avoid having to install it yourself on your computer. To connect to Noto, simply visit https://noto.epfl.ch/ and log in using your standard EPFL account. You are then greeted with a JupyterLab interface: on the left you have a file explorer (these are all private files that only you have access to) and on the right a 'Launcher' that lets you create new objects, such as a terminal instance or a new Python file.

In your file explorer, go to the Documentation folder, and read through the 00_Welcome notebook, which will give you a short overview of how Noto works. As is described there, by default, you already have access to a full Python installation, and could in principle start running code right away. However, it is good practice (and highly recommended) in python to use virtual environments. If you are wondering why this is necessary, you can check out this short webpage or this more in-depth description. Essentially, the idea is that a virtual environment is like a sandbox for a project that lets you install packages without worrying about breaking things because e.g. two packages are incompatible with each other. If something breaks, it's just your sandbox, and your global Python installation is just fine. The 10_Envs_and_Kernels and 11_Tutorial_Envs notebooks in the documentation will guide you through the process of setting up a virtual environment. TL;DR, to create an environment for this course called "astro2", you would open a terminal, and type the following two commands:

my_venvs_create astro2
my_venvs_activate astro2

you will then notice that (astro2) appears before your terminal prompt. This means that you have successfully activated your new environment. If you now install a package, e.g. astropy, using the built-in package manager 'pip' of Python,

pip install astropy

the package will be installed only in your "astro2" environment. To deactivate the environment, you just run the 'deactivate' command. The last thing you need to do is create a kernel for your environment, to be able to use it in Jupyter notebooks. For this, run the following command (as explained in more detail in 10_Envs_and_Kernels) making sure your environment is activated (check that you have the (astro2) in front of your terminal prompt):

my_kernels_create astro2 "Python3 (astro2)"

where the bit in quotes is the name of the kernel, which you can change as you like. Then, simply refresh the page of your browser, and when you click on the blue plus on the left to open the launcher, an option should appear to create new notebooks with your new kernel. You can change the kernel used by an existing notebook in the upper right corner of the notebook window, just left of the white circle. If all of this works, you're good to go!

Note: if you get an error mentioning 'ipykernel' when running the kernel creation command, you may first need to run

pip install ipykernel

again, making sure your environment is currently activated.

Option 2: Install Python locally

If you are using macOS or Linux, you have a python instance installed by default. This 'system python' is however not what we want to use for our python projects, since it is often an older version and is critical for the OS, so it's best to just leave it alone. Instead, to get started and create environments, packages, etc, the first thing we need to do is install a 'user' version of Python on our computer.

There are in fact many options to install python, and people will easily argue about which one is the best. A popular option that is often recommended is Anaconda, a third-party distribution of Python with its own set of tools. The advantage is that it is very streamlined, but a full Anaconda installation is also fairly heavy and contains many components you may not need, such as a graphical user interface and a pre-installed set of packages. If you want to go for this installation approach, we recommend instead to use the lighter alternative Miniconda, which only installs Python and the conda package manager. The installation process and a description of the conda ecosystem is nicely explained on the Anaconda website. Here, we briefly describe how you install python 'natively' without Anaconda, and use the built-in 'venv' tool to manage virtual environments. This is the approach recommended here, but it is mostly a matter of personal taste. Check out the official documentation as well.

Linux

Install python using your distribution's package manager, such as apt or pacman. This will likely install the most recent stable version of Python by default.

macOS

You can either get Python via the installer on the official website, or using the homebrew package manager (which is very useful in general if you plan to do more programming on mac!).

Windows

You can get Python via the installer on the official website, as described in detail here.

Using pyenv

This is the most advanced approach, as it allows you to manage different python versions on your computer in a very clean way. It works on all three systems. For Linux and Mac, you can install pyenv using your system's package manager (homebrew on mac). For Windows, check out this very detailed article.

Once you have Python installed (or you have selected a version of Python if you're using pyenv), you can create a virtual environment using venv. This is in fact what the commands shown above for Noto do in the background! A virtual environment lives in a folder on your computer that can be anywhere in principle. You can either keep your environments right next to your projects, or create a centralized location where all your environments are, which is again what is done on Noto. The process to create an environment is extremely simple. Just run the following commands:

python -m venv /path/to/new/virtual/environment (or python -m venv C:\path\to\new\virtual\environment on windows)

That's it. To enter your new environment, you then run

source /path/to/new/virtual/environment/bin/activate

or, on Windows,

C:\path\to\new\virtual\environment\Scripts\activate.bat (or Activate.ps1 if you're using PowerShell)

This should again prepend your terminal prompt with (name of your environment). To deactivate the environment, as with noto, just type 'deactivate' in your terminal. An environment created like this will be almost empty (i.e. contain no packages). You can install packages with pip. To get the most important ones, which will be also needed in this course, run

python -m pip install numpy matplotlib scipy ipykernel jupyterlab

(again making sure your environment is active). Then, you just need to install a kernel for your virtual environment,

python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

replacing 'myenv' with the name you want for your kernel/environment.

Et voilà! Check out the next page for a selection of good resources to learn your way around the language.