uv: Bad interpreter due to incorrect shebang when running scripts from setuptools setup scripts

Issue

Executing a bin installed from “scripts” tag at setup from setuptools errors

❯ my_package
zsh: /Users/hodeinavarro/Developer/astral.sh/uv-bin-scripts-mre/with-uv/bin/my_package: bad interpreter: /Users/hodeinavarro/Library/Caches/uv/.tmp7Y2T4Y/.venv/bin/python: no such file or directory

MRE

See reproduction and reproduction steps on repo.

Platform

macOS Sonoma 14.4.1 M1 Max zsh & oh-my-zsh & iTerm2

Version

❯ uv --version
uv 0.1.24 (a5cae0292 2024-03-22)

Also confirmed with newest

❯ uv --version
uv 0.1.26 (7b685a815 2024-03-28)

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 5
  • Comments: 20 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@jaraco I have looked at this again.

I can confirm that setuptools’ build_wheel replaces the shebang of scripts to #!python while build_editable sets it to the interpreter used for the build.

pip is not affected by this issue because it does not use a venv to build but the current python with a modified sys.path, so the build interpreter happens to be the desired one, but this is accidental.

IMO, the fix is for setuptools to emit #!python for build_editable too. Note this issue is only for scripts, not for entrypoints, which behave correctly.

Here is the code that Setuptools (and also distutils) uses to rewrite shebangs. It looks like it constructs the executable from sysconfig variables BIN_DIR, VERSION, and EXE. It’s conceivable pip alters those configuration values (BIN_DIR in particular) to affect the rewrite. I traced through the PEP 660 code and it appears to be reaching the build_script command through the build, but that doesn’t explain why the behavior is different for non-editable installs.

Aha, I think I see why. The PEP 517 build uses bdist_wheel to produce the build, which is implemented in the wheel project and specifically sets the executable to "python" when “building” scripts.

That almost certainly explains the disparity. And it sort-of makes sense in that a pure “wheel” is portable and doesn’t know where it’s going to be installed but an “editable wheel” is not portable and presumed to be running from the current environment. That’s consistent with “pip isn’t affected because it builds in the same environment.”

I’m not sure the right thing to do here. My instinct is that the most expedient thing to do would be for UV to rewrite the shebangs in these scripts when moving them. The proper solution is probably more involved and would entail updating the spec to make scripts portable and require installers to rewrite them the same way they do regular. It’s possible the spec already does that and Setuptools is violating it by not re-using the wheel building logic across PEP 517 and PEP 660 builds.

Confirmed, now setuptools specifically sets the executable to “python”. That could make it easier to possibly converge the behavior, although it may still prove impractical to know what’s the best shebang to put there, as it’s only doing a build and not an install. Probably the installers need to take responsibility to rewrite the shebangs.