Live data from Hacker News

Self-contained Python scripts with uv

blog.dusktreader.dev

41–50 of 114 posts

Re: Self-contained Python scripts with uv

#41

Earlier quoted context omitted.

I agree, but I would go a step further. You’re using a magic constant that doesn’t do anything at runtime. It’s only there to be parsed by static analysis. In your case that’s uv doing the parsing but another tool might delete it as unused code. In the sense that it’s one thing pretending to be another, for me, it’s in the same category as a magic comment. Instead, why not make a call to uv telling it what to do?: im…

There's justification for that: https://peps.python.org/pep-0723/#why-not-use-possibly-restr...

I don't agree with it the argumentation.

It might be specified that I needs to be proper JSON. And a proper JSON is much more maintainable (and extendible) than impromptu syntax (that first starts manageable, but step by stem moves into parsing hell).

Re: Self-contained Python scripts with uv

#42

This seems like a good packaging alternative to containerizing for smaller utilities. Now to convince all my coworkers to install uv..

The showstopper for us is our SCA vulnerability scanner doesn't work with uv yet :(

You can add an intermediate sca stage that exports the uv dependencies as requirements.txt

Re: Self-contained Python scripts with uv

#43

We do the same with Nix, the shebang line looks like this: #! nix-shell -i python3 -p "python312.withPackages (pkgs: [ pkgs.boto3 pkgs.click ])" With this, the only requirement is Nix on the system, you don't even need Python to be installed!

Having Nih installed is much stronger requirement than having uv

Re: Self-contained Python scripts with uv

#44

Earlier quoted context omitted.

There's justification for that: https://peps.python.org/pep-0723/#why-not-use-possibly-restr...

Thanks for the link. My counterpoints to the PEP’s arguments are (1) we’re about to run Python so we presumably have a Python parser on hand anyway ; and (2) for the foreseeable future it is going to be capable of parsing all previous versions of Python. It’s a bit fast and loose though. I can see though that it’s helpful for long term stability to have two completely separate languages for dependencies versus the ac…

So but that means instead of uv running python python runs uv now, which (I would imagine) has all kind of implications from a development perspective.

I agree that theoretically your proposed way of doing things would be conceptionally among the cleanest, but on the other hand in all kind of scripts the shebang was sort of a comment with big implications as well, so I am not sure if being dogmatic is worth it here.

Re: Self-contained Python scripts with uv

#45
post #21

My pet peeve (in general, not specific to UV, which I genuinely appreciate) is using comment sections for controlling code execution. Using comments for linters and developer notes is perfectly acceptable. However, for configuration or execution-related data, a far superior pattern would be something like: UV_ENV = { "dependencies": { "requests": "2.32.3", "pandas": "2.2.3" } } This approach has clear advantages: - I…

I agree, but I would go a step further. You’re using a magic constant that doesn’t do anything at runtime. It’s only there to be parsed by static analysis. In your case that’s uv doing the parsing but another tool might delete it as unused code. In the sense that it’s one thing pretending to be another, for me, it’s in the same category as a magic comment. Instead, why not make a call to uv telling it what to do?: im…

Instead, why not make a call to uv telling it what to do?

One important aspect to remember is that this isn't intended to be a uv specific feature. It's a (proposed) python standard feature that in the future other python package managers will implement. So whatever solution they come up with it has to work with any standard compliant package manager, not just uv.

Re: Self-contained Python scripts with uv

#46
post #30
post #16

Earlier quoted context omitted.

I have my own ugly uve script $ cat ~/.local/bin/uve #!/bin/bash temp=$(mktemp) uv export --script $1 --no-hashes > $temp uv run --with-requirements $temp vim $1 unlink $temp Hope editor could support the `uv python find --script` soon.

FYI, have a look at "trap .. EXIT" to defer cleanups like your unlink. It's neat cause it will run even if the script is interrupted / fails before the unlink.

Useful tip, I suppose it will look like:

  temp=$(mktemp)
  trap 'unlink $temp' EXIT
  # Do things

Re: Self-contained Python scripts with uv

#47
post #30

Earlier quoted context omitted.

FYI, have a look at "trap .. EXIT" to defer cleanups like your unlink. It's neat cause it will run even if the script is interrupted / fails before the unlink.

Useful tip, I suppose it will look like: temp=$(mktemp) trap 'unlink $temp' EXIT # Do things

Exactly. And if your cleanup is more involved you can call a cleanup function.

Re: Self-contained Python scripts with uv

#48
post #21

My pet peeve (in general, not specific to UV, which I genuinely appreciate) is using comment sections for controlling code execution. Using comments for linters and developer notes is perfectly acceptable. However, for configuration or execution-related data, a far superior pattern would be something like: UV_ENV = { "dependencies": { "requests": "2.32.3", "pandas": "2.2.3" } } This approach has clear advantages: - I…

I agree, but I would go a step further. You’re using a magic constant that doesn’t do anything at runtime. It’s only there to be parsed by static analysis. In your case that’s uv doing the parsing but another tool might delete it as unused code. In the sense that it’s one thing pretending to be another, for me, it’s in the same category as a magic comment. Instead, why not make a call to uv telling it what to do?: im…

Different python versions have different syntax grammars, so if the rest of your file has new syntax, and older python might not be able to execute even the first few lines.

For example if you run this on python3.6:

    print("hello")
    match 123:
        case _: pass
you won't even get a "hello".

Re: Self-contained Python scripts with uv

#49
post #21

My pet peeve (in general, not specific to UV, which I genuinely appreciate) is using comment sections for controlling code execution. Using comments for linters and developer notes is perfectly acceptable. However, for configuration or execution-related data, a far superior pattern would be something like: UV_ENV = { "dependencies": { "requests": "2.32.3", "pandas": "2.2.3" } } This approach has clear advantages: - I…

I generally agree but it literally uses shebang

Re: Self-contained Python scripts with uv

#50
post #21

My pet peeve (in general, not specific to UV, which I genuinely appreciate) is using comment sections for controlling code execution. Using comments for linters and developer notes is perfectly acceptable. However, for configuration or execution-related data, a far superior pattern would be something like: UV_ENV = { "dependencies": { "requests": "2.32.3", "pandas": "2.2.3" } } This approach has clear advantages: - I…

I agree, but I would go a step further. You’re using a magic constant that doesn’t do anything at runtime. It’s only there to be parsed by static analysis. In your case that’s uv doing the parsing but another tool might delete it as unused code. In the sense that it’s one thing pretending to be another, for me, it’s in the same category as a magic comment. Instead, why not make a call to uv telling it what to do?: im…

That makes your code depend on UV where otherwise it wouldn’t.

Remember the specification to indicate dependencies in a comment on a script is a PEP (723) and it’s tool-agnostic.

Post reply on HN