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.
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.
Recommended Packages
You’ll usually want to include the following in any research conda environment:
numpy: high-performance library for numerical computing.scipy: contains functionality for a wide variety of scientific computing tasks, including optimization and statistics.matplotlib: library for visualization and plotting.
Other packages may make sense depending on your workflow and tasks:
pandas: data analysis and manipulation.polars: an alternative dataframe library, considerably faster thanpandason large tables. Consider it ifpandasis too slow for your data.xarray: allows for structured datasets and interfacing with netCDF.seaborn: adds some additional plotting functionality on top ofmatplotlib, and integrates well withpandas.PyMC: probabilistic programming using a variety of methods, including Hamiltonian Monte Carlo and Metropolis-Hastings. (This was calledpymc3for versions 3 and earlier; the project renamed topymcat version 4, so older tutorials usingimport pymc3need translating.)CmdStanPy: a Python interface to Stan, maintained by the Stan development team.ArviZ: diagnostics, summaries, and plots for Bayesian inference, which works with both of the above. It computes the convergence diagnostics (such as \(\hat{R}\) and effective sample size) that you should check for any MCMC run.PyTorch: machine learning.TensorFlowis an older alternative.mpi4py: bindings for parallelization across multiple processors and compute nodes.mypy: optional static type-checking, can help reduce errors by clarifying expected inputs and outputs and make it easier for others to work with your code.ruff: linting and code formatting in one very fast tool. It replaces theblack+flake8+isortcombination that older guides recommend.
Learn More
- Beyond PEP 8 – Best Practices for Beautiful Intelligible Code by Raymond Hettinger from PyCon 2015.
- Livecoding Madness - Let’s Build a Deep Learning Library by Joel Grus
- Software Testing and Testing Automation with Python
- Python posts from Water Programming.
When You’re Stuck
- 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.
- 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.
- Check which version you have installed, with
pip show <package>orconda list. When the documentation seems wrong, you are often reading about a different version from the one you have. - Search Stack Overflow for the exact error message, with your file paths and variable names removed.
- 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.
- Ask someone, and bring the minimal example, the traceback, and what you have already tried.