Skip to content

Python

Using Jupyter from VS code or Standalone project

VS code

To connect a uv-managed project to a Jupyter notebook within VS Code by creating a kernel of the project VS Code requires ipykernel to be present in the project environment.

# Create a project.
uv init project

# Move into the project directory.
cd project

# Add ipykernel as a dev dependency.
uv add --dev ipykernel

# Open the project in VS Code.
code .

Press + + p and select "Create: New Jupyter Notebook" It creates .ipynb file and select the python kernel right side of the note book

Standalone

If you want to standalone project without need pyproject.toml and uv.lock then run the below commands

1
2
3
4
uv venv --seed
uv pip install pydantic
uv pip install jupyterlab
.venv\Scripts\jupyter lab

It launchs the url http://localhost:8888/lab. you can install additional packages via !uv pip install, or even !pip install. To launch the existing notebook again, .venv\Scripts\jupyter lab

Jupyter Notebook


Create a Python Project Using uv and Manage Dependencies

Create a Python Project

uv init pyprojects

This command automatically creates the following project structure inside the pyprojects folder:

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

pyproject.toml Content

1
2
3
4
5
6
7
[project]
name = "pyprojects"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

Run the Entry Script

uv run main.py

Install Dependencies

Add the latest requests package to pyproject.toml:

uv add requests

This updates the file as follows:

1
2
3
dependencies = [
    "requests>=2.32.5",
]

Upgrade a package:

uv add --upgrade requests

Remove a package:

uv remove requests

Add Development Dependencies

uv add --dev pytest

This creates a [dependency-groups] section in pyproject.toml:

1
2
3
4
[dependency-groups]
dev = [
    "pytest>=8.4.2",
]

Add Dependencies from requirements.txt

uv add -r requirements.txt

List Packages in the Virtual Environment

uv pip list

Lock and Sync

Generate a lock file:

uv lock

Synchronize the environment with the lock file:

uv sync

Python Package and Project Manager Tool – uv

uv is a single tool that replaces pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more.

I’ve mostly used pip, pipenv, and virtualenv in the past, but uv combines most of these tasks into one tool. It’s faster, easier to learn, and highly efficient.

You can find installation instructions and documentation here: https://docs.astral.sh/uv/


Why uv?

  • uv is a Python package and project manager that integrates multiple functionalities into one tool.
  • It enables fast dependency installation, virtual environment management, Python version management, and project initialization, all designed to boost productivity.
  • uv can build and publish Python packages to repositories like PyPI, streamlining the process from development to distribution.
  • It automatically creates and manages virtual environments, ensuring clean and isolated project dependencies.

Usage

Installation

You can install uv from the command line:

  • On Windows, use PowerShell.
  • On macOS/Linux, use curl.
  • You can also install it using pipx:
pipx install uv

Refer to the official documentation for detailed installation options.


Verify installation

uv --version

Upgrade to the latest version

1
2
3
uv self update
# or, if installed via pipx
pipx upgrade uv

Install multiple Python versions

uv python install 3.10 3.11 3.12

Create a virtual environment with a specific version

uv venv --python 3.12.0

Pin a specific Python version in the current directory

uv python pin 3.11

View available and installed Python versions

uv python list

Remove the uv, uvx, and uvw binaries

For Windows,

1
2
3
rm $HOME\.local\bin\uv.exe
rm $HOME\.local\bin\uvx.exe
rm $HOME\.local\bin\uvw.exe
For macOS,
rm ~/.local/bin/uv ~/.local/bin/uvx