Milliams
Milliams

Reputation: 1534

Why does setup.py usually not have a shebang line?

Looking at a random selection of well-known Python packages, why is there a general trend to not include a #!/usr/bin/env python line at the top of setup.py? I know that the usual recommended way of interacting with the file is something like:

python setup.py install

rather than

./setup.py install

but is there a good reason for this?

These packages do not include a shebang: pytest, lxml, six, virtualenv, pip

But these do: requests, simplejson, setuptools

Upvotes: 6

Views: 1426

Answers (4)

Klaus D.
Klaus D.

Reputation: 14369

setup.py is going to do the installation for the Python interpreter you are running it with and its library path. A shebang would define that interpreter and that is not desired by the developer.

Even if you have a setup.py with a shebang, you should still run the file with the interpreter before it. It prevents you from questions like "Where the hack is the package gone!?"

Upvotes: 10

Chong
Chong

Reputation: 963

In my experience, sh in various distributions are almost always located in /bin, or at least in a folder symlinked to /bin. And they almost always follow a same standard. So using #!/bin/sh works in almost all cases, except for on Windows machine, of course. That's something totally different.

Python, on the other hand, is a bit messy on this department. First, not all distributions have python binary stored or symlinked to /bin/python, for example Ubuntu still only have binary of python in /usr/bin/python. On the other hand, even for those which have python in /bin, on some OS, like Fedora and OS X, /bin/python means Python2, while on others, like Arch linux, /bin/python means Python3. Considering Python2 and Python3 codes are not entirely compatible, if using #!/bin/python and run the script with

./setup.py

it could cause issues if python3 was used as interpreter on your machine but really python2 was meant by the script author.

Upvotes: 0

Ami Tavory
Ami Tavory

Reputation: 76297

In this setting, the shebang offers few benefits, and a big drawback:

  • The shebang is useful for reasons that are irrelevant here:

    • It eases using a script repeatedly (less typing)
    • It clarifies how/what a script will do when looking at the source.
  • The shebang necessarily determines the interpreter used to run it. I've written setup.py scripts which I've tested as python setup.py install and python3 setup.py install; in these cases, I simply don't know what to put in the shebang.

Upvotes: 2

minskster
minskster

Reputation: 532

Because python packages must work on Windows platform too. python setup.py install is universal command

Upvotes: 1

Related Questions