Post

Let's try: uv for faster Python packages

uv accommodates the Python project to be faster than ever

Let's try: uv for faster Python packages

These days, I have been using uv and here I would like to share about this.


What is uv

uv is an alternative to pip for Python with the tag line saying that uv is super fast than pip. As far as I have tried, it’s very fast to install packages, for example, pyspark that takes around 3 minutes with pip but several seconds with uv.

I like how quick it performs for installing packages, plus the template files I don’t have to write from scratch, especially “pyproject.toml” that is necessary for building packages.

For the official repo, it’s here.


Alternatives to uv

  • pip: A package installer for Python.
  • conda: Open-source package and environment management. Well-known for data science.
  • poetry: A tool for dependency management and packaging in Python.

Install uv

According to the repo, there are many ways to install uv such as homebrew, curl, pip. I prefer to use devbox.

  • Start from initializing devbox and generating direnv then add uv.

    1
    2
    3
    4
    5
    6
    
    # init devbox and direnv
    devbox init
    devbox generate direnv --force
    
    # add uv into devbox
    devbox add uv
    

    Then edit “devbox.json” to activate Python venv when entering the directory by adding row #7.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    {
      "$schema":  "https://raw.githubusercontent.com/jetify-com/devbox/0.14.0/.schema/devbox.schema.json",
      "packages": ["uv@latest"],
      "shell": {
        "init_hook": [
          "echo 'Welcome to devbox!' > /dev/null",
          "source .venv/bin/activate"
        ],
        "scripts": {
          "test": [
            "echo \"Error: no test specified\" && exit 1"
          ]
        }
      }
    }
    

    Read more about devbox and direnv by following the link below.

  • We can install via curl or pip.

    1
    2
    3
    4
    5
    
    # On macOS and Linux.
    curl -LsSf https://astral.sh/uv/install.sh | sh
    
    # With pip.
    pip install uv
    

    Other solutions can be found below.


Initialize a project

After installation, we need to initialize a project with uv.

1
2
3
4
5
6
7
# format
uv init <flags> <path>

# example
uv init
uv init .
uv init test # create folder `test`

Then we can see new files generated.

1
2
3
4
5
6
7
.
├── .git
├── .gitignore
├── .python-version
├── README.md
├── main.py
└── pyproject.toml

and setup Python with this command.

1
2
3
4
uv python install <version>

# example
uv python install 3.12

Then create venv.

1
uv venv

And we can activate and deactivate the venv.

  • If we install uv through devbox, we don’t have to do it manually.

    devbox should activate it automatically or we would run devbox shell if needed.

  • Run the commands.

    1
    2
    3
    4
    5
    
    # activate
    source .venv/bin/activate
    
    # deactivate
    deactivate
    

add/remove packages

Run this command to install dependencies, or the packages, into “pyproject.toml”. It’s equivalent to pip install.

1
2
3
4
5
6
7
8
9
10
11
# command
uv add <packages>

# example
uv add pandas

# add from requirements.txt
uv add -r requirements.txt

# remove packages
uv remove <packages>

run a script

Execute it by this command.

1
2
3
4
5
# command
uv run <script>

# example
uv run main.py # equivalent to `python3 -m main.py`

update uv

In order to update uv itself, we can follow this.

  • For devbox users, we can update through devbox.

    1
    
    devbox update uv
    
  • If we install uv via the standalone installer, we can do this.

    1
    
    uv self update
    

These are basic commands I currently use a lot with uv.

Hope this helps you guys not to wait for so long in development.

This post is licensed under CC BY 4.0 by the author.