Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have seen many files which have this line as the first line in them. What exactly is this?

#!/usr/bin/env python

What does it mean? Why is it necessary?

share|improve this question
    
The mechanism behind this construct can be found in the execve man page. –  glenn jackman May 6 at 20:02
    
Welcome to Ask Ubuntu. Please, could you put some of your time to read What should I do when someone answers my question? –  Sylvain Pineau May 12 at 11:13
    

4 Answers 4

It's not just /usr/bin/env python but #!/usr/bin/env python and this line is called a shebang.

I'm quoting Wikipedia:

Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.[8] For example, if a script is named with the path "path/to/script", and it starts with the following line:

#!/bin/sh

then the program loader is instructed to run the program "/bin/sh" instead (usually this is the Bourne shell or a compatible shell), passing "path/to/script" as the first argument.

The shebang line is usually ignored by the interpreter because the "#" character is a comment marker in many scripting languages; some language interpreters that do not use the hash mark to begin comments (such as Scheme) still may ignore the shebang line in recognition of its purpose.

In my case (13.10 Desktop), /usr/bin/env python will default to python2.7 but it could be python3.4 depending on your system defaults (e.g. 14.04 Server).

$ /usr/bin/env python
Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
share|improve this answer
    
That means hashbang will be used to tell the program loader that python interpreter is to be called when a script will be run? But, when I create python files, I never put this hasbang on the top. But still that file executes normally. then, what is it? Explain please. –  hellodear May 5 at 9:34
4  
You only need the shebang line if you want your python script to be run like this: ./my_script.py. Without the shebang, you'll have to call python to interpret the script: python ./my_script.py –  Sylvain Pineau May 5 at 9:50
    
@SylvainPineau When running a script residing in the current directory by passing it as a command-line argument to the interpreter, there's no need to precede it's name with ./. python my_script.py works just as well. (It's specifically when a script equipped with a functioning hashbang/shebang line--or other executable--is run as a command in its own right, with its name as the first word on the command line, that ./ is useful.) –  Eliah Kagan Aug 4 at 2:27

A line like this can appear on the first line of interpreted programs or scripts. It is a directive to the program loader to pass the file to the interpreter - in this case Python.

Interpreter directives are placed on the first line of an executable script after the characters #! (called a shebang or hashbang) so that a script can be executed by just the script name, in one of the following ways from the command line:

$ script
$ ./script
$ path/to/script

To be able to execute the script in this way like a command, it must have execute permission.

instead of the interpreter with the script name as an argument which would be required if the interpreter directive was not there, like thus:

$ python path/to/script
share|improve this answer
    
What can be the "path/to/script" ? I mean how can I find it? What is it at your system so that I can get an idea. Thanks. –  hellodear May 5 at 9:39
    
By "path/to/script" I mean the relative or absolute pathname to the script that you are trying to execute. For example, if I am in my home directory and I want to run hello.py in the scripts directory in my home directory then the relative pathname would be scripts/hello.py. –  jamtin May 5 at 11:15

#! is called shebang. Normally, Bash considers the symbol # as a comment, however upon seeing #! Bash knows that the rest of the content should be a script and the first line will refer to which program or interpreter to invoke. In the case of #!/usr/bin/env python Bash knows that this line is invoking the correct "environment settings" for the Python interpreter. Thus the rest of the file's content will be run using the Python interpreter. Had the line been like #!/bin/bash or #!/usr/bin/perl Bash would have run the content using Bash (itself) or Perl respectively.

You can still write a Python, Bash or Perl script without mentioning this line. In that case to run your Python script you'd need to invoke from command line this way:

/usr/bin/python MyScript.py

This is because without shebang #! and without a correct interpreter path, Bash would not know it's a script and would treat it as a text file.

share|improve this answer

If you have several version of python installed on your computer, it ensures that it will use the first one from your $PATH variable.

Alternatively #!/usr/bin/python can also be used which will directly use python interpreter from /usr/bin directory.

share|improve this answer
    
But, why is it used? I can run my python files from terminal easily without this hashbang. what is the difference between files having this hashbang and one without hashbang? Thanks. –  hellodear May 5 at 9:35
    
you can execute you program without specifying the interpreter using ./your_program(after assigning executable permission)in terminal if you are using hasbang in your program. Your program will decide which interpreter to use. But if you are not using hashbang in your program, you need to specify the interpreter explicitly. Advantage of using hashbang is user need not to know in which language script is written to execute it. –  g_p May 5 at 9:54
    
But when I have to run that python file, still I have to mention "python" explicitly, then what is its use? Sorry for putting so many questions but I am confused. You said user don't need to know the language, but it is not executing without mentioning "python". thanks. –  hellodear May 5 at 10:43
    
First of all I would like to tell you that it is #!/usr/bin/env python not #/usr/bin/env python. So change it in your file.Try to assign executable permission to your script using command chmod a+x your_scriptand then use ./your_script to run it. –  g_p May 5 at 12:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.