11Status: published
22Date: 2025-04-30 06:16:38
3- Modified: 2025-07-31 11:06:03
3+ Modified: 2025-11-11 00:29:04
44Author: Benjamin Du
55Slug: manage-python-projects-using-uv
66Title: Manage Python Projects Using uv
@@ -22,57 +22,60 @@ curl -LsSf https://astral.sh/uv/install.sh | env UV_INSTALL_DIR="/usr/local/bin"
2222### Ad-hoc Python Shell & Scripts
2323
24241 . You can start an ad-hoc Python shell (
25- e.g., Python 3.13 with 3rd-party packages dockeree and aituil
25+ e.g., Python 3.14 with 3rd-party packages dockeree and aituil
2626 ) using the following command.
2727
28- uv run --python 3.13 --with dockeree --with aiutil python
28+ uv run --python 3.14 --with dockeree --with aiutil python
2929
30302 . Similar to the above but start an Ipython shell instead.
3131
32- uv run --python 3.13 --with dockeree --with aiutil --with IPython python -m IPython
32+ uv run --python 3.14 --with dockeree --with aiutil --with IPython python -m IPython
3333
34- 2 . Initialize a uv managed Python script.
34+ 3 . Initialize a uv managed Python script.
3535
36- uv init --python 3.13 --script example.py
36+ uv init --python 3.14 --script example.py
3737
3838 Notice that if the script ` example.py ` already exists,
3939 uv will just append metadata lines into it.
4040
41- 3 . You can add dependencies to a uv managed Python scripts
41+ 4 . You can add dependencies to a uv managed Python scripts
4242 using the following command.
4343
4444 uv add --script example.py 'requests<3' 'rich'
4545
46- 2 . You can run a uv managed Python script using
46+ 5 . You can run a uv managed Python script using
4747
48- uv run /path/to/uv_init_pyscript
48+ uv run /path/to/uv_managed_script.py
4949
5050 or
5151
52- uv run --script /path/to/uv_init_pyscript
52+ # preferred
53+ uv run --script /path/to/uv_managed_script.py
5354
5455 Those 2 commands will automatically resolve and install dependencies specified in the script.
5556 This can be simplified by add the shebang
5657 ` #!/usr/bin/env -S uv run `
5758 or ` #!/usr/bin/env -S uv run --script `
5859 into the Python script,
5960 so that you can invoke the script as an executable directly.
60- Notice that there's currently no way to pass the Python interpreter options
61- if you want ` uv ` to automatically resolve and install dependencies of a Python script.
62- You can pass Python interpreter options,
63- say ` -P ` ,
64- using ` uv run python -P ... ` .
61+
62+ 6 . You might want to pass Python interpretor options (e.g., the ` -P ` option)
63+ while running a script using ` uv ` .
64+ This can be done using the command below.
65+
66+ uv run python -P /path/to/uv_managed_script.py
67+
6568 However,
66- when used to run a Python script ( ` uv run python -P /path/to/uv_init_pyscript ` ),
67- it won 't automatically resolve and install dependencies specified in the script.
68- There are 2 ways to address this issue.
69- First,
70- you can configure the corresponding Python environment variable (e.g., ` PYTHONSAFEPATH=1 ` ) if available.
71- Second,
72- Wait for the
73- [ PR ] ( https://github.com/astral-sh/uv/pull/12763 )
74- (which supports ` --with-requirements script.py ` to include inline dependency metadata from another script)
75- to be merged and released.
69+ it has one drawback.
70+ It doesn 't automatically resolve and install dependencies specified in the script.
71+ Luckily,
72+ with [ PR ] ( https://github.com/astral-sh/uv/pull/12763 ) merged,
73+ this is possible through the option ` --with-requirements script_with_deps.py `
74+ which includes inline dependency metadata from ` script_with_deps.py ` .
75+ So,
76+ you can use the following command (though it's verbose).
77+
78+ uv run python -P /path/to/uv_managed_script.py --with-requirements uv_managed_script.py
7679
7780### Manage Projects
7881
0 commit comments