Live data from Hacker News

Uv: Running a script with dependencies

docs.astral.sh

121–130 of 174 posts

Re: Uv: Running a script with dependencies

#121
post #95

Earlier quoted context omitted.

My question is: if you already have to put the "import" there, why not have a PEP to specify the version in that same import statement, and let `uv` scan that dependency? It's something I never understood.

They don't want tools like uv to have to parse Python, which is complex and constantly changes. See also https://peps.python.org/pep-0723/#why-not-infer-the-requirem...

Ahh thanks for that! Makes sense, since you could also have an import somewhere down the script (although that would be bad design).

Another point is the ambiguous naming when having several packages doing roughly the same... Which is crucial here. Thank you! :)

Re: Uv: Running a script with dependencies

#122
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 this has, but so far it's working fine for me, and saved me several gigabytes of storage.

Re: Uv: Running a script with dependencies

#123

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.

I also just activate the desired virtual env and start VS Code from the command line within it

Re: Uv: Running a script with dependencies

#125
post #50

Earlier quoted context omitted.

imo all I usually need is pip-tools and venv, but uv kind of bundles the two together in a very natural way (and is very fast)

Unless you're installing a new version of python, I have trouble seeing how preceding all the normal command with "uv" can be seen as much of a difference.

It's not that much of a difference, but it is faster, and when you do need to switch pythons, you're already set up. uv also has some different workflows, universal lock files, and the caching is also nice. And the PEP 723 support is cool!

Poetry was a big difference, it was also very slow for me, and so I never picked it up.

Re: Uv: Running a script with dependencies

#126
post #23

How many package managers can one language have? Its a simple language but setting it up is just incredibly bad. Maybe this is the one or should I wait for the next?

Its a simple language

It really isn't, and that is why the install and setup tools are so problematic. They have to hide and overcome a lot of complexity.

Re: Uv: Running a script with dependencies

#127
post #110

Earlier quoted context omitted.

Not under OpenBSD.

Thanks for pointing it out. Indeed, OpenBSD’s and NetBSD’s `env` does not support `-S`. DragonflyBSD does (as expected). Solaris as pointed out by the first link doesn’t even support more than one argument in the shebang so no surprise that its `env` does not support it either. Neither does IllumOS (but not sure about the shebang handling)

Also, it doesn't work on Busybox either (e.g. on Alpine Linux).

Re: Uv: Running a script with dependencies

#128

Earlier quoted context omitted.

I really love this and I've been using it a lot. The one thing I'm unsure about is the best way to get my LSP working with inline dependencies. Usually, when I use uv along with a pyproject.toml, I'll activate the venv before starting neovim, and then my LSP (basedpyright) is aware of the dependencies and it all just works. But with inline dependencies, I'm not sure what the best way to do this is. I usually end up j…

Yeah i don't think there is a neater way to do this right now. One thing that maybe saves some effort is a "uv sync" will pay attention to the $VIRTUAL_ENV env var, but only if that var points to a venv that already exists. You can't point to an empty dir and have it create the venv. # make a venv somehow, probably via the editor so it knows about the venv, saving a 3rd step to tell it which venv to use $ env VIRTUAL…

I'd encourage using --dry-run before running uv sync, by default it can be destructive.

  By default, an exact sync is performed: uv removes packages that are not declared as dependencies of the project. Use the `--inexact` flag to keep extraneous packages.

Re: Uv: Running a script with dependencies

#129
post #93

Earlier quoted context omitted.

It IS built-in language syntax. It's defined in the PEP, that's built-in. It's syntax: "Any Python script may have top-level comment blocks that MUST start with the line # /// TYPE where TYPE determines how to process the content. That is: a single #, followed by a single space, followed by three forward slashes, followed by a single space, followed by the type of metadata. Block MUST end with the line # ///. That is…

Semantic whitespace was bad enough, now we have semantic comment blocks? I'm mostly joking, but normally when people say language syntax they mean something outside a comment block.

Shebang lines are semantic comments too

Re: Uv: Running a script with dependencies

#130
post #60
post #23

How many package managers can one language have? Its a simple language but setting it up is just incredibly bad. Maybe this is the one or should I wait for the next?

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.
Post reply on HN