3

I have a package which I am building locally to use inside an embedded environment.

I am trying to generate a console script automatically, so I am using console_scripts inside entry_points like the following:

setup(...,
    entry_points={
        'console_scripts': [
                'app=x.y:main'
        ]
    },
    # Below is added as the other tried methods failed.
    options={
        'build_scripts': {
            'executable': '/bin/custom_python',
        },
    }
)

I am trying to set the python interpreter used in my entry_point as it is different than the one inside the build system. But no matter what I try it keeps set to the local interpreter.

I tried several options like:

  1. Setting an interpreter in shebang header in setup.py
  2. Setting sys.executable inside setup.py
  3. Using options={'build_scripts': {'executable': 'bin/custom_python'}}
  4. Using pip install --global-option=build --global-option='--executable=/bin/custom_python'

But none of the above worked. I wonder if there is something I am missing?

silentnights
  • 813
  • 3
  • 11
  • 21
  • Can you clarify what you want to do? An ``entry_point`` is not actually executed, it is merely a hook for other code to find it. If you mean that you define a ``console_scripts``, that one simply uses the python version used to run ``setup.py``. Just use your desired python version in this case. Overwriting ``sys.executable`` and the like is futile, as your process is already running. – MisterMiyagi Jul 11 '18 at 12:40
  • Sorry if it was not clear. I have am generating the console script via console_scripts option inside entry_points. I have updated the question to clarify this. And the target python version is not available in the host, they are not even in the same path. – silentnights Jul 11 '18 at 13:05
  • I do not understand. ``console_scripts`` is executed when installing, not when building. Whatever version is used on the target host to install, that one is used for ``console_scripts``. How do you "generate" the console scripts? How do you install? – MisterMiyagi Jul 11 '18 at 14:11
  • Let me explain what I am trying to do, I have a package in a mac host which should be build and installed in a directory which will be part of a BSP "Board Support Package" I am trying to automatically generate an entry point for my package which should run x.y.main. The problem here is that the path for the python interpreter is different in the mac host and the target board. I would like to have the entry point point to the correct python interpreter, so I need to somehow force a specific python interpreter in the generated script. – silentnights Jul 11 '18 at 14:21
  • If you do not have access to the regular install machinery (which would create the script for you) build it on your development machine, and replace the shebang in the generated script. – MisterMiyagi Jul 11 '18 at 15:23
  • I assume you've already seen [Changing console_script entry point interpreter for packaging](https://stackoverflow.com/questions/17237878/changing-console-script-entry-point-interpreter-for-packaging)? Also, what Python version are you using for building? AFAIK setting custom shebang for scripts generated from `entry_points` is broken for several years (and versions) now, you can try out the patch suggested in [issue #29411](https://bugs.python.org/issue29411). – hoefling Jul 11 '18 at 18:16
  • @ MisterMiyagi: This is our current solution. I was hoping this was supported by setup tools already since I have read other stackoverflow posts which suggests various solutions. – silentnights Jul 12 '18 at 08:20
  • @hoefling: I am running python-3.6. The link for the patches inside the referenced issue are broken. So what you are saying is that it is currently not supported for entry_points? – silentnights Jul 12 '18 at 08:22
  • It is not supported, although should be. Indeed, all the links are dead (didn't check them). I tried to contact the author of the patches on Github ([link](https://github.com/mcyprian/python3/issues/1)), let's see if he responses. – hoefling Jul 12 '18 at 13:12
  • @hoefling: I have subscribed to the issue you opened. I am looking forward for his answer. Thanks for the help. – silentnights Jul 12 '18 at 14:35
  • @silentnights the author has responded, the patches add/alter the `executable` arg that sets the custom shebang. Turns out the patches are already applied in Fedora, so using this distro with the system `python3` (preinstalled) and `setuptools` (`dnf install python3-setuptools` AFAIK), you should get this working out of the box. Otherwise, organize a separate Python installation (e.g. build from source) and apply the patches. – hoefling Jul 16 '18 at 10:00
  • @hoefling: Thanks for the update. I think building Python is not an option for me now. I will stick with my current solution. I am also investigating why build and install does not respect the options for build_scripts. This looks like an awkward behavior for me. I will debug the build step tomorrow when I am back at work. I think I should find the reason somewhere [here](https://github.com/python/cpython/blob/master/Lib/distutils/command/build_scripts.py) – silentnights Jul 17 '18 at 07:42

2 Answers2

1

So finally I managed to do it.

First I had to add the following to my setup call, which is suggested in various posts:

options={
    'build_scripts': {
        'executable': '/bin/custom_python',
    },
}

Now I build and install the package as usual. In my case, I create a wheel and install it:

python3 setup.py sdist bdist_wheel -d wheels 
pip3 install --no-deps -U --prefix $TARGET_INSTALL wheels/mypackage.whl

The above will use the local python interpreter for the created scripts

To fix that I run the following:

python3 setup.py install_scripts -d $TARGET_INSTALL/bin

which replaces the scripts with the correct ones passed in 'build_scripts' option.

But I am still wondering if there is a way to get the correct interpreter directly when installing the wheel with pip?

silentnights
  • 813
  • 3
  • 11
  • 21
0

Try

python setup.py build --executable /bin/custom_python
python setup.py install  # or bdist_egg/bdist_wheel
phd
  • 82,685
  • 13
  • 120
  • 165