Search code examples
pythonsetuptools

setup.py generates /usr/bin wrappers with a "#!." shebang


I'm trying to package a Python application. The package script runs in a chroot, and invokes setup.py (setuptools-based) as follows:

python setup.py install --root="$pkgdir"

However, the executable wrappers (to be placed in /usr/bin) start with a #!. shebang line, i.e. with a dot instead of the path to the Python interpreter.

Why does this happen and how to work around this (without manually patching the generated scripts)?


Solution

  • setuptools attempts to use the Python interpreter binary used to invoke it for generating its wrappers. To this goal, it invokes distutils, which then queries sys.executable.

    In certain circumstances (such as this particular situation), sys.executable can be an empty string. The most likely reason is that the packaging operation runs in a chroot, which does not allow Python access to /proc.

    os.path.normpath('') evaluates to '.', which then gets propagated all the way back to setuptools, and ends up in a shebang.

    The workaround is to invoke Python using an absolute path:

    /usr/bin/python setup.py install --root="$pkgdir"
    

    Python can then get grab its own path from argv[0], thus allowing setuptools to generate correct shebang lines.