Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip wheel install scripts not respecting parameters after #!python #10661

Open
1 task done
mducle opened this issue Nov 15, 2021 · 8 comments
Open
1 task done

pip wheel install scripts not respecting parameters after #!python #10661

mducle opened this issue Nov 15, 2021 · 8 comments
Labels
C: scripts How scripts and entry points are managed C: wheel The wheel format and 'pip wheel' command PEP implementation Involves some PEP state: awaiting PR Feature discussed, PR is needed type: bug A confirmed bug or unintended behavior

Comments

@mducle
Copy link

mducle commented Nov 15, 2021

Description

I'm not sure if this is a bug or if it's the desired behaviour (I couldn't find any docs on it), but if I create a script with an argument after the interpreter in the shebang, e.g. #!python -i the argument is ignored / missed out when the wheel is installed and the python string is replaced by the real python executable.

This doesn't happen if I install it as an egg.

Expected behavior

I expected that arguments in the shebang line are retained, and the behaviour in eggs and wheels are the same.

pip version

21.3.1

Python version

3.7

OS

Windows 10

How to Reproduce

My setup to reproduce this has the following structure:

top
|-- mypackage
|   |-- __init__.py
|-- scripts
|   |-- interactive_script.py
`-- setup.py

scripts/interactive_script.py is:

#!python -i

import mypackage

mypackage.do_some_preprocessing()

mypackage/__init__.py is:

def do_some_preprocessing():
    print('Done some preprocessing')
    print('Now starting an interactive session')

setup.py is:

from setuptools import setup

setup(
    name='mypackage',
    packages=['mypackage'],
    scripts=['scripts/interactive_script.py']
)

So, I expect that when I run interactive_script.py it would print the two lines and then go into a Python prompt. This is what happens when I do python setup.py install and install an egg.

But if I do python setup.py bdist_wheel or pip wheel . and then install the wheel, it just prints the two lines and exits.

Output

$ python setup.py install
$ interactive_script.py
Done some preprocessing
Now starting an interactive session
>>> quit()

$ pip uninstall mypackage
$ pip wheel .
$ pip install mypackage-0.0.0-py3-none-any.whl
$ interactive_script.py
Done some preprocessing
Now starting an interactive session
$

Code of Conduct

@mducle mducle added S: needs triage Issues/PRs that need to be triaged type: bug A confirmed bug or unintended behavior labels Nov 15, 2021
@uranusjr
Copy link
Member

uranusjr commented Nov 15, 2021

The wheel specification says

In wheel, scripts are packaged in {distribution}-{version}.data/scripts/. If the first line of a file in scripts/ starts with exactly b'#!python', rewrite to point to the correct interpreter. Unix installers may need to add the +x bit to these files if the archive was created on Windows.

The problem is how you interpret “rewrite to point to the correct interpreter”. pip currently replaces the entire shebang line, which is arguably wrong. A more sensible approach would be to split by first place (typo: space) and append whatever is after it back to the new shebang.

(Note that this approach is not guaranteed to work because spaces in shebang is very poorly specified without any way to produce a universally protable solution. But that is also a problem with the current implementation as well, and splitting by the first space at least would not break currently-working scenarios.)

@uranusjr uranusjr added C: scripts How scripts and entry points are managed C: wheel The wheel format and 'pip wheel' command state: awaiting PR Feature discussed, PR is needed and removed S: needs triage Issues/PRs that need to be triaged labels Nov 15, 2021
@mducle
Copy link
Author

mducle commented Nov 15, 2021

@uranusjr Thanks for the explanation. I wasn't quite sure whether it was a bug or whether it was the intended behaviour, but I would support splitting the string by the first space and replacing the first part... This seems to be what the build_scripts class in distutils currently do, so I think that's why there's different behaviour between the egg and wheel installations.

@uranusjr
Copy link
Member

Would you be interested in submitting a pull request fixing this?

@uranusjr
Copy link
Member

Also @pradyunsg since we’d probably want to make sure installer is handling this the same way.

@pfmoore
Copy link
Member

pfmoore commented Nov 15, 2021

... and maybe update the wheel spec to be clearer on the intended interpretation.

@pradyunsg
Copy link
Member

... and maybe update the wheel spec to be clearer on the intended interpretation.

That. :)

@pradyunsg
Copy link
Member

@mducle
Copy link
Author

mducle commented Nov 16, 2021

Would you be interested in submitting a pull request fixing this?

Sure... I've create one now - I hope it's ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: scripts How scripts and entry points are managed C: wheel The wheel format and 'pip wheel' command PEP implementation Involves some PEP state: awaiting PR Feature discussed, PR is needed type: bug A confirmed bug or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants