Search code examples
pythonpippypi

Why does `pip install` seem to change the interpreter line on some machines?


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?


Solution

  • 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".