10

I am working on a medium sized python (2.7) project with multiple files I import. I have one main python file which starts the program. Other files contain class definitions, functions, etc.

I was wondering if I should put the shebang line in every python file or only the one I run in order to start my program?

Oliver W.
  • 13,169
  • 3
  • 37
  • 50
ap0
  • 1,083
  • 1
  • 12
  • 37

5 Answers5

14

Only files which you will execute by invoking them directly require the shebang. Small hint: if a file contains

if __name__ == "__main__":
    main()

it is better (to stick with the Least Astonishment principle) to start it with a shebang. Make sure to have that shebang robust; rather have

#!/usr/bin/env python

than

#!/usr/bin/python

Many module files contain a main method used to start tests, so many module files start with a shebang.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
  • Warning Will Robinson! `env` relies on the PATH environment variable, which could be insecure. It could invoke a different version of Python to the one you are expecting. Both options here have their downsides. – cdarke Oct 30 '14 at 10:46
  • If the code relies on a specific version of Python, it should test the version at the beginning. The whole point of using `env` is to honor the `PATH` variable and make sure the interpreter that is started is the one that is intended by the user. – damienfrancois Oct 30 '14 at 10:55
  • 3
    @cdarke: Using `env` should *never* do something you don't expect. It will run the `python` in your `PATH`, which should not be at all surprising (I'm not sure how this would be surprising to anyone). If you want it to use a different `python`, you should just invoke that `python` with the script called as an argument, which the shebang will not override. – Nick Bastin Oct 30 '14 at 10:57
  • @NickBastin: we will have to disagree on that. I consider anything set by the user as tainted. It's not 'your' PATH you get, but 'their' PATH. See also http://cwe.mitre.org/data/definitions/426.html and many, many, others. – cdarke Oct 30 '14 at 13:07
  • @cdarke In which setting do you consider the user's PATH an 'externally supplied search path?' In my viewpoint, the user running the script trusts its own PATH variable. – damienfrancois Oct 30 '14 at 15:28
  • @cdarke: The user owns the box, their expectation is all that matters. If you have developed your app and assume it will not be run by a malicious python, or a compromised libc, or any other of a million options you can't control, you've done it wrong. – Nick Bastin Oct 30 '14 at 22:02
  • @NickBastin: perhaps you should be asking yourself why the ShellShock vulnerability is such a concern. – cdarke Nov 01 '14 at 11:22
  • @cdarke: that is a completely specious comment - there is no relationship between ShellShock and any kind of `PATH` expectation on the part of the user or the shell. If the application developer makes any assumptions about the `PATH` of the user (and the shared runtime libraries they will link against) they have made a fatal design flaw irrespective of whether `env` will run a trusted `python`. – Nick Bastin Nov 01 '14 at 14:06
  • 1
    Apparently, the shebang `#!/usr/bin/env ...` is more robust: http://stackoverflow.com/a/5549091/1959808 The suggested one doesn't exist by default on OSX (and OSX is quite common an operating system). – 0 _ Apr 10 '15 at 00:06
3

It depends. The short answer is that you only need the #! line if the file is to be executed as a main program. You might take that as meaning only one file needs it. But....

It is a common pattern to write modules for use as both a component and a main program. This (at the very least) can aid testing. The module can be tested on its own without the rest of the program. The trick is to put this at the end of the program:

if __name__ == '__main__':
    # Run some tests, or other stuff here

That way, it can exist as a main program. It is very common to have a function called main and call it from the if statement, but that is not mandatory.

cdarke
  • 42,728
  • 8
  • 80
  • 84
2

You only need it in the file you execute, though it might help code editors tell what kind of code they are looking at if you have it in other files too.

khelwood
  • 55,782
  • 14
  • 81
  • 108
1

No, only the main Python file needs the shebang.

The shebang is only needed if you want to execute it as ./your_file.py or as your_file.py if it's in your $PATH. So unless the other files should also be executable by themselves (you can always execute using python your_file.py) you don't need the shebang.

Wolph
  • 78,177
  • 11
  • 137
  • 148
1

Python scripts with "executable" permissions (or any executable script files on unix, linux, mac, etc) can use a shebang line. It's important to understand that Python does not read any lines marked as a comment (prefixed with a "#" sign) and that includes a shebang line. Actually, your OS reads the shebang line and sends the script off to the appropriate interpreter as instructed by the shebang line. lots of scripts do this: "sh" scripts "bash" scripts "python" scripts, all of which are interpreted.

So, only the executable Python files that you want to run as command/programs, including package files coded with: if __name__ == '__main__' somewhere in the script, which are also intended to be executed directly by a user.

(the objective of the shebang line it to simplify the scripts execution. Rather than forcing the user to run the program by typing the interpreter to use, and then typing the script file as an argument. The user can just type in the command only, as if it were any other command, or possible just click it on the desktop. The OS will then read the script first, find the shebang line, then call the interpreter found in the shebang)

user12711
  • 693
  • 1
  • 7
  • 21