astral-sh / uv

An extremely fast Python package and project manager, written in Rust.
https://docs.astral.sh/uv
Apache License 2.0
23.08k stars 669 forks source link

Support interactive script invocations #8064

Open zanieb opened 1 week ago

zanieb commented 1 week ago

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_

Michallote commented 1 week ago

wow that was fast!

This was tested with

$ uv version
uv 0.4.18