astral-sh / uv

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

Suggestion: `uv bundle`, `uv build --release` or similar to create a contained executable a la pyinstaller, py2exe #5802

Open matterhorn103 opened 1 month ago

matterhorn103 commented 1 month ago

I regularly use pyinstaller for a project, and the frequency of questions about it - or the task more generally of "compiling" an executable for distribution - around the web indicates to me that it's widely popular.

It would be incredibly cool if uv offered a uv bundle command (or whatever name) that essentially did what pyinstaller does and bundles together everything to afford an executable that contains the dependencies and interpreter and can be distributed to users as-is. And I think this falls into the realm of what things a "Cargo for Python" might be expected to be able to do.

I assume that pyinstaller, py2exe etc. are complex projects and implementing such functionality would be not trivial in the slightest, so I would have thought it would be a case of using pyinstaller itself in the background like the way that build backends are used.

Presumably there will one day be a uv build command but I assume that will be similar to rye build in that it will create an sdist or wheel. Maybe there could be a uv build --executable option, or uv build --release (by analogy to cargo build --release)?

matterhorn103 commented 1 month ago

Some relevant links that I'm adding to keep track of them for myself as much as anyone else:

chrisrodrigue commented 1 month ago

To add… despite its popularity, pyinstaller suffers from slowness because has to unzip all the dependencies and the interpreter when the executable is launched. It offers users the option to display a splash image to distract them from the perceived slowness.

chrisrodrigue commented 1 month ago

Python to Rust transpilation would be pretty cool and there’s at least one project out there working on this:

Imagine writing idiomatic Python, transpiling it to Rust and compiling a native executable. My mind would be blown.

I don’t know how one could gracefully handle dependencies though. A lot of Python libs incorporate C extensions. Dealing with those would be extremely difficult, but there’s a DARPA program called TRACTOR working on that.

paveldikov commented 1 month ago

An equivalent of pyinstaller that actually understands standard pyproject.toml-driven projects and console/GUI script entrypoints (instead of being fixated on script development, and overcompensating with its dependency detection magic) would be huge.

(somewhat related: #5653)

my1e5 commented 1 month ago

I want to give a mention to Nuitka which is serving me well. It's as straightforward as running

uv add --dev nuitka
uv run python -m nuitka --standalone src/main.py

which spits out an executable.

It's not really the right tool to be including with uv, it is essentially an alternate Python interpreter which compiles Python to C. But it's very easy to add and has worked well for me. And my experience with the developers has been very good. They were quick to add support for Rye when I encountered bugs - https://github.com/Nuitka/Nuitka/issues/2933

matterhorn103 commented 3 weeks ago

@my1e5 why do you say that Nuitka would be unsuitable for inclusion with uv?

my1e5 commented 2 weeks ago

@matterhorn103, I guess what I mean is that Nuitka is perhaps quite an opinionated way of packaging your Python code into an executable - as it is essentially an alternate Python interpreter which compiles Python to C. Whereas tools like Pyinstaller are more like a bundler - taking the Python interpreter, Python files and dependencies and packaging them into an executable. This is definitely more straightforward than the Nuitka approach, but it does make the Pyinstaller executable very easy to un-package and see the underlying source code (see https://github.com/extremecoders-re/pyinstxtractor). Which might be a caveat needed when including certain 'executable creators' within uv - you need to make users aware that their exe can easily be un-packaged.

In terms of licensing, Nuitka is MIT licensed. But it does also have a commercial tier - which may or may not complicate matters if Astral were to want to bundle it with uv, I don't know.

zanieb commented 1 week ago

I think this is a duplicate of https://github.com/astral-sh/uv/issues/2799, though there's more discussion here.

martimlobao commented 1 week ago

Also want to call out pex, a tool for generating .pex files (Python EXecutable) which are self-contained zipped executable Python environments containing sources, requirements, and dependencies.

https://github.com/pex-tool/pex