# Python

## Overview: Python interpreter + UV

To work effectively with Python, you need two components:

1. **A Python interpreter**: This is the core Python installation that runs your code.
2. **UV**: A modern package and project manager that handles installing libraries,
   managing dependencies, and creating isolated environments for your projects.

### Why UV?

UV solves several critical problems in Python development:

- **Fast and reliable package installation**: UV is significantly faster than traditional
  tools like pip, especially when installing scientific packages.
- **Dependency management**: It ensures that all packages work together correctly and
  creates a reproducible environment.
- **Project isolation**: UV creates separate environments for each project, preventing
  conflicts between different projects' requirements.
- **Modern workflow**: UV combines the functionality of several older tools into one
  consistent interface.

While there are other package managers (we'll also install Pixi for conda packages), UV
is currently the best choice for managing Python packages from PyPI, which includes most
of the scientific Python ecosystem.

### Why not only UV then?

UV can actually install and manage Python interpreters itself without needing a separate
installation. However, relying exclusively on UV-managed Python has some limitations in
certain use cases, particularly around system integration and compatibility with specific
tools. Installing a standalone Python interpreter alongside UV provides greater
flexibility and ensures compatibility across different workflows.

## Install a Python interpreter

:::::{tab-set}
::::{tab-item} Windows
On Windows, use the Python install manager to install Python. You have two options:

1. **Microsoft Store** (recommended): Install "Python" from the
   [Microsoft Store](https://apps.microsoft.com/detail/9NQ7512CXL7T)
2. **WinGet**: Run `winget install 9NQ7512CXL7T` in PowerShell or Command Prompt

Once installed, you can install Python 3.13 (or any other version) using:

```powershell
py install 3.13
```

The install manager uses the `py` command and will automatically update itself. You can
see all available Python versions with `py list --online`.
::::

::::{tab-item} macOS
On macOS, the simplest way to install Python is by using `brew`:

```sh
brew install python
```

Alternatively, you can install Python by following
[the instructions on this official page](https://docs.python.org/3/using/mac.html#using-python-for-macos-from-python-org)
(open this link in a new tab).
::::

::::{tab-item} Debian-Ubuntu-Mint
On Linux, a Python interpreter is typically already installed. However, for Python
development, it is helpful to install some additional components:

```sh
sudo apt install python3-dev python3-venv
```

:::{admonition} Why python3-venv?
Most Linux distributions package Python in modular components, and it is possible to
install Python without certain standard library packages. The `venv` package, which
creates virtual environments, is particularly important. In Debian/Ubuntu, this
functionality is provided by the `python3-venv` package. Virtual environments are
essential for isolating project dependencies, so installing this package is highly
recommended.
:::

:::{tip}
The packages `python3-full` can also be useful for certain workflows.

However, avoid installing `python3-pip`, as UV provides a better alternative for package
management.
:::
::::
:::::

:::{note}
If you have installed WSL (Windows Subsystem for Linux), you should install Python on
both the Windows side and the Ubuntu-WSL side. This allows you to work flexibly in both
environments.
:::

:::{admonition} Alternative installation methods
For specific use cases or advanced workflows, you might consider other Python
installation methods such as [pyenv](https://github.com/pyenv/pyenv) (for managing
multiple Python versions) or [Guix](https://guix.gnu.org) (for reproducible system
configurations).
:::

## UV: Modern Python package and project manager

[UV] is a fast, modern Python package and project manager developed in Rust by Astral. It
has quickly become the recommended tool for managing Python packages and projects due to
its speed and reliability.

### What UV does for you

- **Installs Python packages** quickly and reliably (much faster than pip)
- **Manages project dependencies** and ensures they're compatible
- **Creates isolated environments** automatically for each project
- **Handles Python versions** so you can use different Python versions for different
  projects
- **Locks dependencies** to ensure your project works the same way everywhere

### Installation

To install [UV], open a terminal and run

:::::{tab-set}
::::{tab-item} Windows
```powershell
powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
```
::::

::::{tab-item} macOS and Linux
```sh
curl -LsSf https://astral.sh/uv/install.sh | sh
```
::::
:::::

Once the installation is complete, open a new terminal and verify that the `uv` command
is available:

```sh
uv --version
```

You should see the UV version number displayed.

:::{note}
For more details about the installation, see the
[UV documentation](https://docs.astral.sh/uv/getting-started/installation/).
:::

:::{tip}
You'll use UV throughout the training for tasks like:

- `uv init` to create new Python projects
- `uv add numpy pandas` to install packages
- `uv run python script.py` to run scripts in isolated environments
- `uv tool install` to install command-line tools globally

Don't worry if these commands seem unfamiliar. We'll practice them during the training.
:::

[uv]: https://docs.astral.sh/uv/
