02 - Getting Started with Python and uv
#
Lesson Overview#
The goal of this section is to gain a basic understanding of:
What Python is.
How to install it (we will use
uv
) and where it “lives” on your computer.How to run Python code.
What packages are and how to install them.
What virtual environments are, why they are useful, and how to create them.
We will focus on using uv
to manage Python versions,
create virtual environments, and install packages.
Slides#
Notes#
Here is a summary of some of the most useful commands and concepts that we cover in the lecture for this section.
Terminal commands#
Here are some basic terminal commands that will help you use the
command line effectively. These commands work for macOS,
Linux, and the Windows PowerShell terminal (but not the windows cmd
Prompt).
Tip
Reminder, To open the terminal:
On macOS, you can use the
Command + Space
shortcut to open Spotlight, then type “Terminal” and hitEnter
.On Windows, you can use the
Windows + R
shortcut to open the Run dialog, then type “powershell” and hitEnter
.
Command |
Description |
---|---|
|
List directory contents. |
|
Change directory to |
|
Print the current working directory. |
|
Create a new directory named |
Tip
When using relative paths, you can use ..
to refer to the parent directory,
and .
to refer to the current directory.
uv
cheat sheet#
uv
is a fast, one-stop-shop for managing
Python versions, virtual environments, and packages. It is an excellent tool
to learn if you want to get started quickly with Python.
Here are some common uv
commands that will help you get started using Python
with uv
. We will use them throughout this book.
Command |
Description |
---|---|
|
Useful way to get help on a specific |
|
Create a virtual environment at the specified |
|
Activate the virtual environment in the path |
|
Install or uninstall |
|
List all installed packages in the current virtual environment. |
|
Run a Python script |
Run a |
|
|
Use the specified version of python (e.g. |
… For complete docs, see Getting started with uv.
Note
While we will not be covering it here, uv
is also frequently used for
project management. If you
see references to commands like uv init
, uv sync
, uv add
, or uv remove
,
these are only relevant in the context of using uv
for project management.
uv
↔️ conda
translation table#
Tip
If you are already familiar with conda
, here is a quick translation table to
help you understand how uv
commands map to conda
commands:
|
|
Description |
---|---|---|
|
|
Create a new env |
|
|
Activate the virtual environment in the path |
|
|
Install a package into the env |
|
|
Remove a package from the env |
|
|
List all packages in the env |
uv run
#
uv run
is a command that allows you to run a command or script in an on-demand
virtual environment.
If you have a simple python script named hello.py
, that has no dependencies:
print("Hello, world!")
You can run it with:
uv run hello.py
… which is shorthand for uv run python hello.py
.
… For complete docs, see Running scripts.
uv run
with additional dependencies#
A particulary useful feature of uv run
is that you can specify dependencies
in your script using a special syntax in a comment at the top of the file.
For example, the following example_script.py
uses two packages, requests
and rich
, to download and print a JSON file downloaded from the internet in a
nice format:
# /// script
# dependencies = ["requests<3", "rich"]
# ///
import requests
from rich import print
resp = requests.get("https://jsonplaceholder.typicode.com/posts")
print(resp.json())
With uv
you can run this script without having to first create an
environment and install the necessary dependencies, simply
by running:
uv run example_script.py
… For complete docs, see Declaring script dependencies.
Running Jupyter notebooks with juv
#
💡 Think of juv
as uv run
for Jupyter notebooks.
juv
is a command line tool built on top of uv
that provides a
convenient way to run a Jupyter notebook, with all the necessary
dependencies, in an on-demand virtual environment.
It is built on the following concepts:
As we saw above:
uvx juv
is shorthand for “run the commandjuv
from the packagejuv
.As we saw above:
uv
can read dependencies specified inside of a script.juv
just brings along the necessary Jupyter notebook dependencies and parses the dependencies from the top of your notebook foruv
.
Command |
Description |
---|---|
|
Initialize a new Jupyter notebook named |
|
Add a new dependency to the comment at the top of |
|
Launch a Jupyter notebook server and run the notebook |
… For complete docs, see the juv
repository.