Platform R: Python Package Management Using uv with Conda

 

uv is a drop-in replacement for tools like pip, virtualenv, etc. It is much faster than existing Python package managers, which is most useful when you have many pure python dependencies to resolve for your software.

Conda is still the better option for some packages that depend on system-level libraries, such as GDAL. But you can use Conda and uv together.

  • Pure Python and Native + PyPI (such as numpy, scipy, pandas): uv will work well
  • PyTorch: uv will work for base PyTorch, but if you need GPU/CUDA support, conda is best
  • GDAL: conda is best
  • R + Python interoperability: conda is best

If you try to add a package with uv and it fails, try installing with conda.

Python Versions

uv can also manage different versions of Python, but the mechanism for that doesn't work in the air-gapped Platform R environment. You have to set up your environment with Conda first using the version of Python you want, and then add uv to that Conda environment.

Installing uv and Conda Together

You can create a new Conda environment with uv already installed:

conda create --name uvtest python=3.13 uv

After you run conda activate uvtest you can start using uv. With uv you would also normally create a uv environment. If you don't want to install into a separate uv environment, you can use uv pip install instead, which will use uv to update your currently active conda environment. However, it will not do the package locking uv would otherwise do.

Setting up a uv Environment

The steps below assume you've created and activated the uvtest environment from the example above.

  1. Create a new directory and initialize a uv environment:
    mkdir uvpandas
    cd uvpandas
    uv init
  2. Add whatever packages you need:
    uv add pandas
  3. Activate the uv environment:
    . .venv/bin/activate
    After this step your shell prompt will have (uvpandas) (uvtest) at the start, showing the active Conda and uv environments.
  4. Now you can start python (with python3) and import Pandas (import pandas).

Use the command deactivate to leave a uv environment, and then conda deactivate to leave the Conda environment.

Starting Scripts with uv

Python currently supports metadata in scripts. uv can use this metadata to ensure that the correct package versions are available to a script. uv can create a script with the metadata headers already in place:

uv init --script example.py --python 3.13

Because of the Platform R limitations on letting uv manage Python versions, the Python version will need to match the version you picked for the conda environment uv is running in. If you skip the --python option, it will add a dependency for the current or later version. The resulting example.py script will look like this:

# /// script
# requires-python = ">=3.13"
# dependencies = []
# ///

def main() -> None:
    print("Hello from example.py!")

if __name__ == "__main__":
    main()

The lines # /// script and # /// surround the metadata and should not be touched. You can add to dependencies, which is a list of strings with package names and optional versions, such as:

# /// script
# requires-python = ">=3.13"
# dependencies = [
#   "numpy>1.24.0",
#   "rich>=13.0.0",
# ]
# ///

When you run this script with uv run example.py any missing packages will be downloaded at the correct version.

Using uv Environments in a Slurm Batch Job

Using the uvtest environment created above, here is an example batch file which activates both the conda and uv environments:

#!/bin/bash
#SBATCH --partition=cpu
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
#SBATCH --time=1:00
#SBATCH --mem=16G

# Clear out any active environments first.
module purge
conda deactivate

conda activate uvtest
. $HOME/uvpandas/.venv/bin/activate

srun uv run example.py


Keywords:
platform r, conda, python, uv
Doc ID:
162550
Owned by:
William A. in SMPH Research Applications
Created:
2026-07-09
Updated:
2026-08-28
Sites:
SMPH Research Applications