uv icon indicating copy to clipboard operation
uv copied to clipboard

Support interactive script invocations

Open zanieb opened this issue 1 year ago • 5 comments

In: main.py

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "numpy",
#     "pandas",
# ]
# ///

import numpy
import pandas

def main():
    pass

if __name__ == '__main__':
    main()
    print('Done!')

Then using uv produces no errors:

user@pop-os:~/Documents/GitHub/standalone_scripts$ uv run main.py 
Reading inline script metadata from: main.py
Done!

However when trying to get a REPL after an execution (maybe inspect some variables or have imports ready):

user@pop-os:~/Documents/GitHub/standalone_scripts$ uv run python -i main.py
Traceback (most recent call last):
  File "/home/user/Documents/GitHub/standalone_scripts/main.py", line 9, in <module>
    import numpy
ModuleNotFoundError: No module named 'numpy'
>>> 

Originally posted by @Michallote in https://github.com/astral-sh/uv/issues/6548#issuecomment-2403714219_

zanieb avatar Oct 10 '24 01:10 zanieb

wow that was fast!

This was tested with

$ uv version
uv 0.4.18

Michallote avatar Oct 10 '24 01:10 Michallote

Also worth mentioning uv run ipython -i ./main.py and uv run bpython -i ./main.py

SnirBroshi avatar Dec 15 '24 01:12 SnirBroshi

$ # run [i/b]python in isolated environment with resolved script dependencies
$ uv run --with-script main.py python -i main.py
$ uv run --with-script main.py ipython -i ./main.py
$ uv run --with-script main.py bpython -i ./main.py

T-256 avatar Dec 15 '24 10:12 T-256

Is it possible to open a python repl with a particular python version and package? ie

uv run python --python 3.10 --packages boto3,requests

I often find I want to do this when trying out a new package real quick.

josiahcoad avatar Dec 17 '24 02:12 josiahcoad

uv run  --python 3.10 --with boto3 --with requests python

charliermarsh avatar Dec 17 '24 02:12 charliermarsh

uv run --with-script main.py python -i main.py

unfortunately as of uv 0.7.6 this option is gone; I couldn't find a replacement after a quick glance at uv run --help; is there a way to drop back to REPL after the script finishes? Something like uv run --script -i script.py

billyzs avatar May 22 '25 08:05 billyzs

Does uv sync --script script.py work for your case?

konstin avatar May 22 '25 18:05 konstin

Does uv sync --script script.py work for your case?

(Assuming this is directed to me) Somewhat indirectly; My setup is that I have a script.py (which declares its deps at the top thanks to uv)in a uv managed project directory; the script is for some exploratory works so I'm not ready to introduce the script deps to the project yet. uv sync --script script.py actually replaced project deps with script deps in the project venv; if I want to go back to working on the project I'll have to effectively rebuild the venv for the project. The rebuild is fast because uv and uv devs are awesome, but the process still feels a bit clunky. What I propose is something like uv run --interactive script.py that drops me back to the REPL so I can keep playing with local variables, like python -i script.py would do.

Anyways, I managed to achieve this effect for now by manually adding a breakpoint() at the end of the script. Still would be nice to get an official solution. wdyt?

billyzs avatar May 23 '25 05:05 billyzs

uv sync --script script.py does not handle requires-python. it would be great if --with-script was reinstated, or if uv run could just accept the full set of python arguments (-i, -O, -OO, -u, -v, -W, -X)

technillogue avatar Nov 02 '25 19:11 technillogue