uv and juv#
To go through these exercises, you need to create a new folder on your Desktop called bobiac_uv.
Exercise 1#
Create a Python Environment with uv and Install numpy & matplotlib
Open the Terminal or PowerShell and navigate to the
bobiac_uvfolder.Create a new virtual environment with
uvin thebobiac_uv_envfolder namedmy-env.Activate the newly created
my-envvirtual environment.Install
numpyandmatplotlibin themy-envvirtual environment.List the installed packages in the
my-envvirtual environment to confirm thatnumpyandmatplotlibare installed.Deactivate the
my-envvirtual environment.
Solution
open the Terminal or PowerShell.
cd .../Desktop/bobiac_uvuv venv my-envsource my-env/bin/activate(Linux/macOS) ormy-env\Scripts\activate(Windows)uv pip install numpy matplotlibuv pip listdeactivate
Exercise 2#
Create a new Jupyter Notebook with uv’s juv tool
Open the Terminal or PowerShell and navigate to the
bobiac_uvfolder.Inside the
bobiac_uvfolder, create a new folder namednb_folderand navigate into it.Inside the
nb_folder, create a new Jupyter Notebook namedmy-first-nb.ipynbusinguv’sjuvtool.Run the
my-first-nb.ipynbnotebook in your browser usingjuv.
Solution
open the Terminal or PowerShell.
cd .../Desktop/bobiac_uvmkdir nb_foldercd .../Desktop/nb_folderuvx juv init my-first-nb.ipynbuvx juv run my-first-nb.ipynb
Exercise 3#
Add dependencies to a Jupyter Notebook with juv
In the
my-first-nb.ipynbnotebook created in Exercise 2, addmatplotlib,numpy, andpandasas dependencies usingjuv.Run the notebook with
juvand import the three libraries in the first cell to confirm that they are installed and can be imported without errors.Note
If you have the
my-first-nb.ipynbnotebook open in your browser, you need to stop it from the terminal (cmd + corctrl + c) and close the browser tab before running the notebook again withjuvor the new dependencies will not be installed.
Solution
cd .../Desktop/nb_folder(the folder created in Exercise 2)uvx juv add my-first-nb.ipynb numpy pandas matplotlibuvx juv run my-first-nb.ipynbadd a new
codecell in the notebook (+button)within this cell, import the three libraries:
import matplotlib import numpy as np import pandas as pd
Run the cell to confirm that the libraries are imported without errors.
Exercise 4#
Create a .py file with the PEP723 ///script header using uv
Open the Terminal or PowerShell and navigate to the
bobiac_uvfolder.Inside the
bobiac_uvfolder, create a new folder namedpy_folderand navigate into it.Use
uvto create a new Python file namedmy_script.pywith the PEP723///scriptheader and Python3.13.Add
numpyandmatplotlibas dependencies to themy_script.pyfile usinguv.Run the
my_script.pyfile usinguvto verify that the dependencies are installed and the script runs without errors.Optional: show the dependencies of the
my_script.pyfile usinguv tree.Optional: show the python version used for the
my_script.pyfile usinguv python.
Solution
open the Terminal or PowerShell.
cd .../Desktop/bobiac_uvmkdir py_foldercd .../Desktop/bobiac_uv/py_folderuv init --script my_script.py -p 3.13uv add --script my_script.py numpy matplotlibuv run my_script.pyOptional:
uv tree --script my_script.pyOptional:
uv python find --script my_script.py --show-version