Skip to main content

Python

Python is a general-purpose programming language, aimed at striking a balance between performance and code interpretability. Python has some high-performance numerical libraries, but is not as performant as languages like C++. However, it is widely used, the code can be easy to read, and it has a large collection of packages.

Installation

Do not use the Python that came with your operating system for research work. Install and manage your own, so that each project gets an isolated, reproducible environment.

There are two reasonable ways to do this:

  • conda (installed through Miniforge), which also installs non-Python dependencies. The geospatial and climate libraries (GDAL, PROJ, netCDF) need those, which is why conda is our default.
  • uv, a much faster package and version manager for projects that only need Python packages.

Pick one per project and commit its lockfile or environment file to the repository. Whichever you use, avoid installing into a base environment.

Some packages may not be available through conda; in that case use pip from within the activated conda environment, and record them in your environment file.

Usage

A good place to start if you don’t know any Python is the Python Fundamentals chapter from Earth and Environmental Data Science. There are also a number of other resources available online, and many specific questions can be or already are answered on Stack Overflow.

Python is a dynamically-typed language, meaning that it automatically assigns types to variables and functions. This means that, by default, it lacks type checking, which can result in bugs. Dynamic typing is also one reason why Python is more user-friendly than a statically-typed language like C++, but is also slower.

Development Environments

  • VS Code with the Python extension is the group default. It is free, handles notebooks and remote work over SSH (useful for Hopper), and is the same editor we recommend for Julia, so you only have to learn one.
  • PyCharm: a full-featured IDE from JetBrains, which offers free licenses to students.
  • Spyder: lightweight, free, open-source, with a MATLAB-like layout that some people coming from MATLAB prefer.
NoteAtom

Earlier versions of this page recommended Atom. GitHub retired Atom in December 2022, and it no longer receives updates, including security updates. If you are still using it, please switch.

Learn More

When You’re Stuck

  1. Read the traceback from the bottom up. The last line says what failed, and the lines above show how the code got there. The problem is usually in the first frame that is in your own code, not in a library.
  2. Read the official documentation and the library’s own documentation before searching the web. Library documentation is generally good, and search results often describe older versions.
  3. Check which version you have installed, with pip show <package> or conda list. When the documentation seems wrong, you are often reading about a different version from the one you have.
  4. Search Stack Overflow for the exact error message, with your file paths and variable names removed.
  5. Make a minimal example: cut the problem down to the smallest script that still fails. This often solves the problem, and if it doesn’t, you have something small enough for someone else to look at.
  6. Ask someone, and bring the minimal example, the traceback, and what you have already tried.