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

Most upvoted comments

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:

#!/usr/bin/env python
''' module docstring '''
from __future__ import print_function

because uv/uvx turns it into:

#!/bin/sh
'''exec' "$(dirname -- "$(realpath -- "$0")")"/'python' "$0" "$@"
' '''
''' module docstring '''
from __future__ import print_function

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:

#!/usr/bin/env python
from __future__ import print_function
''' module docstring '''

because uv/uvx turns it into:

#!/bin/sh
'''exec' "$(dirname -- "$(realpath -- "$0")")"/'python' "$0" "$@"
' '''
from __future__ import print_function
''' module docstring '''

which is ugly and may confuse help(), but at least it is not a SyntaxError.

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 .exe which invokes a python script.

So for relocatable virtualenvs instead of having bin/tool be the script, you would generate bin/tool-script as an executable wrapper that then invokes bin/tool-script.py (which itself is not marked as executable).

bin/tool then looks like this:

#!/bin/sh
"exec" "`dirname $0`/python" "$0" "$@"
__file__ = __import__('os').path.join(__import__('os').path.dirname(__file__), "tool-script.py")
exec(compile((lambda f: (f.read(), f.close())[0])(open(__file__)), __file__, 'exec'))

and bin/tool-script.py is the normal file unchanged (at least for as long as it’s a python script).

This results in the following:

  • line numbers are unchanged as the script is unchanged
  • __file__ reports tool-script.py instead of tool
  • the frame code’s co_filename reports tool-script.py instead of tool
  • __doc__ is correct (needed for libraries such as docopt)
  • The script combine docstrings with __future__ imports

I think this would be the best tradeoff but it comes with the cost of generating a useless second file. The only requirement is sh which is a standard tool. Because sh uses exec it will also not keep a second process running around for nothing.

Like the current solution if someone were to run file on the wrapper bin/tool script 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 underlying tool-script.py file will correctly detect Python which is an improvement over today.