uv
uv copied to clipboard
Support interactive script invocations
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_
wow that was fast!
This was tested with
$ uv version
uv 0.4.18
Also worth mentioning uv run ipython -i ./main.py and uv run bpython -i ./main.py
$ # 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
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.
uv run --python 3.10 --with boto3 --with requests python
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
Does uv sync --script script.py work for your case?
Does
uv sync --script script.pywork 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?
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)