Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script header

The typical header should be

#!/usr/bin/env python 

But I found below also works when executing the script like $python ./my_script.py

#!/usr/bin/python #!python 

What's difference between these 2 headers? What could be the problem for 2nd one? Please also discussing the case for python interpreter is in PATH or not. Thanks.

like image 887
Stan Avatar asked Jun 27 '10 19:06

Stan


People also ask

How do I make a header in Python?

You can create headings by starting and ending a line with up to five equal signs. The heading text is between those markers, separated by a single space.

What is a header in Python code?

Header comments appear at the top of a file. These lines typically include the filename, author, date, version number, and a description of what the file is for and what it contains. For class assignments, headers should also include such things as course name, number, section, instructor, and assignment number.

Are there headers in Python?

Practical Data Science using PythonHeaders contain protocol specific information that appear at the beginning of the raw message that is sent over TCP connection. The body of the message is separated from headers using a blank line.

What do I write at the top of a Python script?

The first line of all your Python programs should be a shebang line, which tells your computer that you want Python to execute this program. The shebang line begins with #! , but the rest depends on your operating system. On Windows, the shebang line is #! python3 .


2 Answers

First, any time you run a script using the interpreter explicitly, as in

$ python ./my_script.py $ ksh ~/bin/redouble.sh $ lua5.1 /usr/local/bin/osbf3 

the #! line is always ignored. The #! line is a Unix feature of executable scripts only, and you can see it documented in full on the man page for execve(2). There you will find that the word following #! must be the pathname of a valid executable. So

#!/usr/bin/env python 

executes whatever python is on the users $PATH. This form is resilient to the Python interpreter being moved around, which makes it somewhat more portable, but it also means that the user can override the standard Python interpreter by putting something ahead of it in $PATH. Depending on your goals, this behavior may or may not be OK.

Next,

#!/usr/bin/python 

deals with the common case that a Python interpreter is installed in /usr/bin. If it's installed somewhere else, you lose. But this is a good way to ensure you get exactly the version you want or else nothing at all ("fail-stop" behavior), as in

#!/usr/bin/python2.5 

Finally,

#!python 

works only if there is a python executable in the current directory when the script is run. Not recommended.

like image 162
Norman Ramsey Avatar answered Sep 22 '22 14:09

Norman Ramsey



I'd suggest 3 things in the beginning of your script:

First, as already being said use environment:

#!/usr/bin/env python 

Second, set your encoding:

# -*- coding: utf-8 -*- 

Third, set some doc string:

"""This is a awesome     python script!""" 

And for sure I would use " " (4 spaces) for ident.
Final header will look like:

#!/usr/bin/env python # -*- coding: utf-8 -*-  """This is a awesome         python script!""" 


Best wishes and happy coding.

like image 39
Dimitar Atanasov Avatar answered Sep 21 '22 14:09

Dimitar Atanasov