Search code examples
pythonsetuptoolsdistutils

Changing console_script entry point interpreter for packaging


I'm packaging some python packages using a well known third party packaging system, and I'm encountering an issue with the way entry points are created.

When I install an entry point on my machine, the entry point will contain a shebang pointed at whatever python interpreter, like so:

in /home/me/development/test/setup.py

from setuptools import setup
setup(
    entry_points={
        "console_scripts": [
            'some-entry-point = test:main',
        ]
    }
)        

in /home/me/.virtualenvs/test/bin/some-entry-point:

#!/home/me/.virtualenvs/test/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: 'test==1.0.0','console_scripts','some-entry-point'
__requires__ = 'test==1.0.0'
import sys
from pkg_resources import load_entry_point

sys.exit(
   load_entry_point('test==1.0.0', 'console_scripts', 'some-entry-point')()
)

As you can see, the entry point boilerplate contains a hard-coded path to the python interpreter that's in the virtual environment that I'm using to create my third party package.

Installing this entry point using my third-party packaging system results in the entry point being installed on the machine. However, with this hard-coded reference to a python interpreter which doesn't exist on the target machine, the user must run python /path/to/some-entry-point.

The shebang makes this pretty unportable. (which isn't a design goal of virtualenv for sure; but I just need to MAKE it a little more portable here.)

I'd rather not resort to crazed find/xargs/sed commands. (Although that's my fallback.)

Is there some way that I can change the interpreter path after the shebang using setuptools flags or configs?


Solution

  • You can customize the console_scripts' shebang line by setting 'sys.executable' (learned this from a debian bug report). That is to say...

    sys.executable = '/bin/custom_python'
    
    setup(
      entry_points={
        'console_scripts': [
           ... etc...
        ]
      }
    )
    

    Better though would be to include the 'execute' argument when building...

    setup(
      entry_points={
        'console_scripts': [
           ... etc...
        ]
      },
      options={
          'build_scripts': {
              'executable': '/bin/custom_python',
          },
      }
    )