Live data from Hacker News

Uv: Running a script with dependencies

docs.astral.sh

131–140 of 174 posts

Re: Uv: Running a script with dependencies

#131
post #4

The "declaring script dependencies" thing is incredibly useful: https://docs.astral.sh/uv/guides/scripts/#declaring-script-d... # /// script # dependencies = [ # "requests Save that as script.py and you can use "uv run script.py" to run it with the specified dependencies, magically installed into a temporary virtual environment without you having to think about them at all. It's an implementation of Python PEP 723: h…

> Save that as script.py and you can use "uv run script.py" to run it with the specified dependencies, Be aware that uv will create a full copy of that environment for each script by default. Depending on your number of scripts, this could become wasteful really fast. There is a flag "--link-mode symlink" which will link the dependencies from the cache. I'm not sure why this isn't the default, or which disadvantages…

By default it will create hard links for python packages, so it won't consume any more memory (besides the small overhead of hard links).

Re: Uv: Running a script with dependencies

#132
post #120

I wish there was a straightforward way to let VS Code pick up the venv that uv transparently creates. Out of the box, the Python extension redlines all the third-party imports. As a workaround, I have to plunge into the guts of uv's Cache directory to tell VS Code the cached venv path manually, and cross fingers that it won't recreate that venv too often.

Could you tell me how you do this in code ? Started using uv in a very old school team and they don’t like uv simply because it’s new. Something like this is what they base it on.

It's super cumbersome so I can't really recommend it for work.

I invoke the "Select Interpreter" action. A file selector opens, then I go to the user cache directory (e.g. ~/.cache on Linux, something like %LOCALAPPDATA%\Cache on Windows). It has a `uv` subdirectory, then I drill further down until I find the directory where uv keeps its venvs. Find the venv that corresponds to your script, then go to its `bin` subdirectory and select the Python executable.

The upside is that you only have to do this once per script.

The downside is that you have to do this once per script.

Re: Uv: Running a script with dependencies

#133
post #131

Earlier quoted context omitted.

> Save that as script.py and you can use "uv run script.py" to run it with the specified dependencies, Be aware that uv will create a full copy of that environment for each script by default. Depending on your number of scripts, this could become wasteful really fast. There is a flag "--link-mode symlink" which will link the dependencies from the cache. I'm not sure why this isn't the default, or which disadvantages…

By default it will create hard links for python packages, so it won't consume any more memory (besides the small overhead of hard links).

Unless it can't because you happen to have mounted your user cache directory from a different volume in an attempt to debloat your hourly backups.

Re: Uv: Running a script with dependencies

#134
post #77

Earlier quoted context omitted.

uv is so cool. haha. goodbye conda.

conda is a completely no-go for me. It’s for data scientists, and only data scientists who do not need to care much about production.

Sadly this isn't always true, if you have non-pure Python dependencies, you might still need to use conda since they package database drivers or complex C/C++ dependencies for example.

Re: Uv: Running a script with dependencies

#135
post #60

Earlier quoted context omitted.

I had the same sentiment, but uv seems to have eliminated the competition. Installing uv using your OS package manager is enough as it can also download and install (isolated) Python interpreters as well.

Except pip still exists and is default, and even if they adopted uv as default instead, all the legacy stuff takes years to change.

pip is not a package manager, it's only meant for installing packages. Also, uv has a pip-compliant interface through `uv pip` which lets you interface with pip easily.

Re: Uv: Running a script with dependencies

#136
post #83

Earlier quoted context omitted.

Note that it's not really "cleaned up" insofar as there is a uv cache folder that will grow bigger over time as you keep using that feature.

True. It's a good idea to periodically run: uv cache clean Or, if you want to specifically clean just jupyter: uv cache clean jupyter

if anyone else is curious..

  % cd $(uv cache dir); find . -type f | wc -l; du -hs .
  234495
  16G .

Re: Uv: Running a script with dependencies

#137

Earlier quoted context omitted.

Except pip still exists and is default, and even if they adopted uv as default instead, all the legacy stuff takes years to change.

pip is not a package manager, it's only meant for installing packages. Also, uv has a pip-compliant interface through `uv pip` which lets you interface with pip easily.

Those shouldn't be separate things. uv is both. Anyway, the default package manager is pipenv https://packaging.python.org/en/latest/tutorials/managing-de...

Re: Uv: Running a script with dependencies

#138

Earlier quoted context omitted.

pip is not a package manager, it's only meant for installing packages. Also, uv has a pip-compliant interface through `uv pip` which lets you interface with pip easily.

Those shouldn't be separate things. uv is both. Anyway, the default package manager is pipenv https://packaging.python.org/en/latest/tutorials/managing-de...

You do you. I'm happy with uv.

Re: Uv: Running a script with dependencies

#139
post #4

The "declaring script dependencies" thing is incredibly useful: https://docs.astral.sh/uv/guides/scripts/#declaring-script-d... # /// script # dependencies = [ # "requests Save that as script.py and you can use "uv run script.py" to run it with the specified dependencies, magically installed into a temporary virtual environment without you having to think about them at all. It's an implementation of Python PEP 723: h…

PEP723 is also supported by pipx and hatch, even though uv is (deservedly) getting all the attention these days. Others like pip-tools have support in the roadmap ( https://github.com/jazzband/pip-tools/issues/2027 )

I plan to support it in PAPER as well. A big part of the original rationale for PEP 723 (and 722) was to support people who want to distribute a single Python file to colleagues etc. without worrying about Python packaging (or making a "project"), and allow them to run it with the appropriate dependencies and their own local Python. So while I am trying to make a tool for users (that incidentally covers a portion of developer use cases) rather than developers, this definitely fits.

Re: Uv: Running a script with dependencies

#140
post #131

Earlier quoted context omitted.

By default it will create hard links for python packages, so it won't consume any more memory (besides the small overhead of hard links).

Unless it can't because you happen to have mounted your user cache directory from a different volume in an attempt to debloat your hourly backups.

... your hourly backups aren't also using the same hard-linking strategy?
Post reply on HN