Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do people write #!/usr/bin/env python on the first line of a Python script?

I see this at the top of Python files:

  1. For Python 2 files
    #!/usr/bin/env python 
  2. For Python 3 files
    #!/usr/bin/env python3 

It seems to me like the files run the same without that line.

like image 329
john garcias Avatar asked Mar 11 '10 23:03

john garcias


People also ask

What is the purpose of writing?

These four purposes of writing - to entertain, inform, persuade, and express feelings - are often referred to as the four core purposes.

What are the 4 reasons to write?

There are four purposes writers use for writing. When someone communicates ideas in writing, they usually do so to express themselves, inform their reader, to persuade a reader or to create a literary work.

Why do some people like to write?

I love to write because it allows my mind to go to anyplace it wants to with no restrictions. Writing, I've found,is a way to express myself in ways I may not otherwise be comfortable doing. I enjoy writing because I love being able to express myself and my own ideas. I enjoy writing because it has so much freedom.

Why do some writers write?

5% of Writers Write because it is a passion and pleasure. Writing is a creative passion that gives me great pleasure. I enjoy living in the imaginary world of my characters and creating a world of my own making. it also forms an outlet for my urge to tell my professional life's story, only told in novel form.


2 Answers

If you have several versions of Python installed, /usr/bin/env will ensure the interpreter used is the first one on your environment's $PATH. The alternative would be to hardcode something like #!/usr/bin/python; that's ok, but less flexible.

In Unix, an executable file that's meant to be interpreted can indicate what interpreter to use by having a #! at the start of the first line, followed by the interpreter (and any flags it may need).

If you're talking about other platforms, of course, this rule does not apply (but that "shebang line" does no harm, and will help if you ever copy that script to a platform with a Unix base, such as Linux, Mac, etc).

like image 116
Alex Martelli Avatar answered Sep 19 '22 07:09

Alex Martelli


That is called the shebang line. As the Wikipedia entry explains:

In computing, a shebang (also called a hashbang, hashpling, pound bang, or crunchbang) refers to the characters "#!" when they are the first two characters in an interpreter directive as the first line of a text file. In a Unix-like operating system, the program loader takes the presence of these two characters as an indication that the file is a script, and tries to execute that script using the interpreter specified by the rest of the first line in the file.

See also the Unix FAQ entry.

Even on Windows, where the shebang line does not determine the interpreter to be run, you can pass options to the interpreter by specifying them on the shebang line. I find it useful to keep a generic shebang line in one-off scripts (such as the ones I write when answering questions on SO), so I can quickly test them on both Windows and ArchLinux.

The env utility allows you to invoke a command on the path:

The first remaining argument specifies the program name to invoke; it is searched for according to the PATH environment variable. Any remaining arguments are passed as arguments to that program.

like image 42
Sinan Ünür Avatar answered Sep 19 '22 07:09

Sinan Ünür