uv: Scripts that use "from future" can result in syntax error when used with uvx
When using uvx with script files that have from future import and shebang with a space, I receive the following syntax error:
from __future__ import print_function, unicode_literals
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: from __future__ imports must occur at the beginning of the file
Example:
uvx tabview
Link to shebang in tabview, link to uv snippet that causes the syntax error.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 15 (10 by maintainers)
Commits related to this issue
- chore: Ported project to pyproject.toml and pytest change: Marked wheel as Python 3 only (no longer universal) — committed to bottlepy/bottle by defnull 2 years ago
- mise, .local/bin: Switch pipx bins to mise Aargh, more bugs (not in mise though): https://github.com/astral-sh/uv/issues/6489 https://github.com/bottlepy/bottle/commit/c92516069e664efc9bac3746004402b... — committed to liskin/dotfiles by liskin 2 years ago
- fix: Workaround for uv/uvx bug The uv/uvx tools break scripts that contain a __future__ import by injecting a multiline string infront of it. Future imports are very limited on where they can appear.... — committed to bottlepy/bottle by defnull a year ago
If you are maintaining a module or tool affected by this, there is a workaround: Move the
from __future__ import ...line above your own module docstring. It must be the very first non-comment statement in the file. This way, the string injected by uv/uvx will count as the new module-docstring and not as a forbidden statements before the future-import. As long as there is only one string before the future-import, the parser won’t complain.This does not work:
because uv/uvx turns it into:
which is a
SyntaxError. There are two string statements in front of the future-import and only the first one counts as the module-docstring.This works:
because uv/uvx turns it into:
which is ugly and may confuse
help(), but at least it is not aSyntaxError.I think we could make it non-relocatable, though the issue will still be present for long shebangs or paths with spaces, right?
Yes, I think both the “spaces” case and the “too long” case could occur relatively rarely, but the use of relocatable envs makes this impact everyone who attempts to use tabview (or any other script with a docstring that uses
__future__imports) with uvx. So the impact of relocatable is much broader.Dumping my thoughts from another conversation here: I think patching the actual script is always going to result in frustration, a better option would be to generate a trampoline script that imports the actual code. Something similar is already done on windows where you get a
.exewhich invokes a python script.So for relocatable virtualenvs instead of having
bin/toolbe the script, you would generatebin/tool-scriptas an executable wrapper that then invokesbin/tool-script.py(which itself is not marked as executable).bin/toolthen looks like this:and
bin/tool-script.pyis the normal file unchanged (at least for as long as it’s a python script).This results in the following:
__file__reportstool-script.pyinstead oftoolco_filenamereportstool-script.pyinstead oftool__doc__is correct (needed for libraries such asdocopt)__future__importsI think this would be the best tradeoff but it comes with the cost of generating a useless second file. The only requirement is
shwhich is a standard tool. Becauseshusesexecit will also not keep a second process running around for nothing.Like the current solution if someone were to run
fileon the wrapperbin/toolscript it would misclassify the file as perl, but since that wrapper is kinda useless that’s probably okay. For most editors actually opening up the underlyingtool-script.pyfile will correctly detect Python which is an improvement over today.