Mengelola Environment dan Pustaka Python Menggunakan uv

Posted in: Software  
Tags: Programming   Python   Environment Manager   Package Manager  
Author: | | 5 minutes reading time

Jika anda adalah developer Python yang serius, environment manager adalah keharusan. Environment manager memungkinkan developer untuk menginstall lebih dari satu versi Python karena “mengganggu” instalasi Python di default sistem operasi bisa mengacaukan. Beberapa environment manager di Python sudah banyak dibuat dan sampai saat ini saya setia dengan Micromamba. Hari ini saya ingin mencoba uv yang mulai banyak digunakan. uv dibuat menggunakan Rust dan berfungsi sebagai environment manager serta package manager.

Instalasi uv

$ curl -LsSf https://astral.sh/uv/install.sh | sh
downloading uv 0.7.3 x86_64-unknown-linux-gnu
no checksums to verify
installing to /home/bpdp/.local/bin
  uv
  uvx
everything's installed!
$

Menjalankan uv

$ uv
An extremely fast Python package manager.

Usage: uv [OPTIONS] <COMMAND>

Commands:
  run      Run a command or script
  init     Create a new project
  add      Add dependencies to the project
  remove   Remove dependencies from the project
  sync     Update the project's environment
  lock     Update the project's lockfile
  export   Export the project's lockfile to an alternate format
  tree     Display the project's dependency tree
  tool     Run and install commands provided by Python packages
  python   Manage Python versions and installations
  pip      Manage Python packages with a pip-compatible interface
  venv     Create a virtual environment
  build    Build Python packages into source distributions and wheels
  publish  Upload distributions to an index
  cache    Manage uv's cache
  self     Manage the uv executable
  version  Read or update the project's version
  help     Display documentation for a command

Cache options:
  -n, --no-cache               Avoid reading from or writing to the cache, instead using a temporary directory for the duration of the
                               operation [env: UV_NO_CACHE=]
      --cache-dir <CACHE_DIR>  Path to the cache directory [env: UV_CACHE_DIR=]

Python options:
  --managed-python       Require use of uv-managed Python versions [env: UV_MANAGED_PYTHON=]
  --no-managed-python    Disable use of uv-managed Python versions [env: UV_NO_MANAGED_PYTHON=]
  --no-python-downloads  Disable automatic downloads of Python. [env: "UV_PYTHON_DOWNLOADS=never"]

Global options:
  -q, --quiet...                                   Use quiet output
  -v, --verbose...                                 Use verbose output
      --color <COLOR_CHOICE>                       Control the use of color in output [possible values: auto, always, never]
      --native-tls                                 Whether to load TLS certificates from the platform's native certificate store [env:
                                                   UV_NATIVE_TLS=]
      --offline                                    Disable network access [env: UV_OFFLINE=]
      --allow-insecure-host <ALLOW_INSECURE_HOST>  Allow insecure connections to a host [env: UV_INSECURE_HOST=]
      --no-progress                                Hide all progress outputs [env: UV_NO_PROGRESS=]
      --directory <DIRECTORY>                      Change to the given directory prior to running the command
      --project <PROJECT>                          Run the command within the given project directory [env: UV_PROJECT=]
      --config-file <CONFIG_FILE>                  The path to a `uv.toml` file to use for configuration [env: UV_CONFIG_FILE=]
      --no-config                                  Avoid discovering configuration files (`pyproject.toml`, `uv.toml`) [env:
                                                   UV_NO_CONFIG=]
  -h, --help                                       Display the concise help for this command
  -V, --version                                    Display the uv version

Use `uv help` for more details.
$

Menginstall Python dan mengaktifkan Python versi tertentu

$ uv python install 3.12
Installed Python 3.12.10 in 10.01s
 + cpython-3.12.10-linux-x86_64-gnu
$

Menampilkan Daftar Instalasi Python

$ uv python list
cpython-3.14.0a6-linux-x86_64-gnu                 <download available>
cpython-3.14.0a6+freethreaded-linux-x86_64-gnu    <download available>
cpython-3.13.3-linux-x86_64-gnu                   /usr/bin/python3.13
cpython-3.13.3-linux-x86_64-gnu                   /usr/bin/python3 -> python3.13
cpython-3.13.3-linux-x86_64-gnu                   /usr/bin/python -> python3.13
cpython-3.13.3-linux-x86_64-gnu                   <download available>
cpython-3.13.3+freethreaded-linux-x86_64-gnu      <download available>
cpython-3.12.10-linux-x86_64-gnu                  .local/share/uv/python/cpython-3.12.10-linux-x86_64-gnu/bin/python3.12
cpython-3.11.12-linux-x86_64-gnu                  <download available>
cpython-3.10.17-linux-x86_64-gnu                  <download available>
cpython-3.9.22-linux-x86_64-gnu                   <download available>
cpython-3.8.20-linux-x86_64-gnu                   <download available>
pypy-3.11.11-linux-x86_64-gnu                     <download available>
pypy-3.10.16-linux-x86_64-gnu                     <download available>
pypy-3.9.19-linux-x86_64-gnu                      <download available>
pypy-3.8.16-linux-x86_64-gnu                      <download available>
graalpy-3.11.0-linux-x86_64-gnu                   <download available>
graalpy-3.10.0-linux-x86_64-gnu                   <download available>
graalpy-3.8.5-linux-x86_64-gnu                    <download available>
$

Menggunakan Environment

Saat belum diaktifkan, menggunakan instalasi default sistem:

$ python
Python 3.13.3 (main, Apr 11 2025, 13:41:26) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Mengubah menggunakan versi yang diinstall menggunakan uv:

$ uv venv py312-agentic --python 3.12
Using CPython 3.12.10
Creating virtual environment at: py312-agentic
Activate with: source py312-agentic/bin/activate.fish
$ source py312-agentic/bin/activate.fish 
(py312-agentic) $ python
Python 3.12.10 (main, Apr  9 2025, 04:03:51) [Clang 20.1.0 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
$

Isi dari direktori py312-agentic adalah sebagai berikut:

$ tree py312-agentic -L2
py312-agentic
├── CACHEDIR.TAG
├── bin
│   ├── activate
│   ├── activate.bat
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate.nu
│   ├── activate.ps1
│   ├── activate_this.py
│   ├── cowsay
│   ├── deactivate.bat
│   ├── pydoc.bat
│   ├── python -> /home/bpdp/.local/share/uv/python/cpython-3.12.10-linux-x86_64-gnu/bin/python3.12
│   ├── python3 -> python
│   └── python3.12 -> python
├── lib
│   └── python3.12
├── lib64 -> lib
└── pyvenv.cfg

5 directories, 15 files
$

Untuk keluar dari environment:

$ deactivate
$

Install Paket Pustaka

$ uv pip list
Using Python 3.12.10 environment at: py312-agentic
(py312-agentic) $ uv pip install cowsay
Using Python 3.12.10 environment at: py312-agentic
Resolved 1 package in 2ms
Installed 1 package in 3ms
 + cowsay==6.1
(py312-agentic) $ uv pip list
Using Python 3.12.10 environment at: py312-agentic
Package Version
------- -------
cowsay  6.1
(py312-agentic) $ ls -la py312-agentic/lib/python3.12/site-packages/
total 32
drwxr-xr-x 5 bpdp bpdp 4096 May  8 22:26 ./
drwxr-xr-x 3 bpdp bpdp 4096 May  8 22:02 ../
drwxr-xr-x 2 bpdp bpdp 4096 May  8 22:24 __pycache__/
-rw-r--r-- 1 bpdp bpdp   18 May  8 22:02 _virtualenv.pth
-rw-r--r-- 1 bpdp bpdp 4342 May  8 22:02 _virtualenv.py
drwxr-xr-x 4 bpdp bpdp 4096 May  8 22:27 cowsay/
drwxr-xr-x 2 bpdp bpdp 4096 May  8 22:26 cowsay-6.1.dist-info/
(py312-agentic) $ python
Python 3.12.10 (main, Apr  9 2025, 04:03:51) [Clang 20.1.0 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cowsay
>>> cowsay.cow('Hi!')
  ___
| Hi! |
  ===
   \
    \
      ^__^
      (oo)\_______
      (__)\       )\/\
          ||----w |
          ||     ||
>>> 

Setelah keluar dari environment tersebut, paket yang sudah diinstall pada environment tersebut sudah tidak bisa diakses lagi.

$ deactivate
$ python
Python 3.13.3 (main, Apr 11 2025, 13:41:26) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cowsay
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    import cowsay
ModuleNotFoundError: No module named 'cowsay'
>>> 

Penutup

Selain sangat cepat, saya cenderung melihat uv sebagai solusi yang lebih sederhana untuk kebutuhan pengelolaan environment dan packages. Instalasi paket juga lebih memudahkan kalau sudah terbiasa menggunakan pip. Berbeda dengan conda serta micromamba yang cenderung menggunakan instalasi Python lengkap untuk setiap environment, uv melakukan instalasi Python (sesuai versi yang dikehendaki) dan kemudian menyimpan berbagai paket di direktori nama-environment/lib/python-3.12/site-packages.