3

I put my colorize.py package onto PyPi and discovered the following strange behavior. If I download the colorize.py-0.9.1.tar.gz from the project page, and I examine the first line of colorize.py, I get the following output:

$ wget https://files.pythonhosted.org/packages/91/d8/805853c14a8ccf67ddfe2cf41b634395ef69a1138a0dade303bf4b7c9b45/colorize.py-0.9.1.tar.gz
$ tar xvfz colorize.py-0.9.1.tar.gz
$ head -n1 colorize.py-0.9.1/colorize.py
#!/usr/bin/env python

However, on the same my Ubuntu 16.04.4 LTS laptop, if I install using pip, the first line is has been replaced:

$ pip install colorize.py
$ head -n1 $(which colorize.py)
#!/usr/bin/python

On the other hand, my friend runs the same installation command on her OS X laptop, and gets the original interpreter line.

What is going on here, and is it possible to force pip to not change the interpreter line when performing installation?

merlin2011
  • 71,677
  • 44
  • 195
  • 329
  • Does setup.py use console entry point? If so, the command line script is generated at install time. – Jonathon Reinhart May 28 '18 at 01:15
  • @JonathonReinhart, I'm not sure how to determine that; I didn't do anything specific to make it use the console entry point, but I'm also not sure if it's done automatically. It just calls `setup` from `setuptools`: https://github.com/hq6/Colorize/blob/master/setup.py – merlin2011 May 28 '18 at 01:18

1 Answers1

4

This is done deliberately: if you have multiple Python installations, running this file as an executable will always invoke the Python that it was installed for, regardless of your current PATH. See pytest running with another version of python for a case where this makes a difference -- specifically, prevents breakage of system scripts that are Python-based.

pip/_vendor/distlib/scripts.py:_make_script() is the code that does the job (look for "shebang").

There's absolutely no reason (thus no provided way) to disable this functionality: running a script with a different installation than what it was installed for is practically guaranteed to break it. If you really want it, you can always run <different python> <path_to_script.py>. As Python's motto goes: "make right things easy, make wrong things hard".

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
  • 1
    Thanks for finding the code that actually does this. :) – merlin2011 May 28 '18 at 02:15
  • @merlin2011 Just searched `pip`'s dir for "shebang", and the right results came up. A few reference searches later, got the whole picture. Was actually quite easy this time) – ivan_pozdeev May 28 '18 at 02:23