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

后端 未结 21 1755
刺人心
刺人心 2020-11-21 06:16

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

相关标签:
21条回答
  • 2020-11-21 06:51

    Expanding a bit on the other answers, here's a little example of how your command line scripts can get into trouble by incautious use of /usr/bin/env shebang lines:

    $ /usr/local/bin/python -V
    Python 2.6.4
    $ /usr/bin/python -V
    Python 2.5.1
    $ cat my_script.py 
    #!/usr/bin/env python
    import json
    print "hello, json"
    $ PATH=/usr/local/bin:/usr/bin
    $ ./my_script.py 
    hello, json
    $ PATH=/usr/bin:/usr/local/bin
    $ ./my_script.py 
    Traceback (most recent call last):
      File "./my_script.py", line 2, in <module>
        import json
    ImportError: No module named json
    

    The json module doesn't exist in Python 2.5.

    One way to guard against that kind of problem is to use the versioned python command names that are typically installed with most Pythons:

    $ cat my_script.py 
    #!/usr/bin/env python2.6
    import json
    print "hello, json"
    

    If you just need to distinguish between Python 2.x and Python 3.x, recent releases of Python 3 also provide a python3 name:

    $ cat my_script.py 
    #!/usr/bin/env python3
    import json
    print("hello, json")
    
    0 讨论(0)
  • 2020-11-21 06:52

    It probably makes sense to emphasize one thing that the most have missed, which may prevent immediate understanding. When you type python in terminal you don't normally provide a full path. Instead, the executable is up looked in PATH environment variable. In turn, when you want to execute a Python program directly, /path/to/app.py, one must tell the shell what interpreter to use (via the hashbang, what the other contributors are explaining above).

    Hashbang expects full path to an interpreter. Thus to run your Python program directly you have to provide full path to Python binary which varies significantly, especially considering a use of virtualenv. To address portability the trick with /usr/bin/env is used. The latter is originally intended to alter environment in-place and run a command in it. When no alteration is provided it runs the command in current environment, which effectively results in the same PATH lookup which does the trick.

    Source from unix stackexchange

    0 讨论(0)
  • 2020-11-21 06:52

    It just specifies what interpreter you want to use. To understand this, create a file through terminal by doing touch test.py, then type into that file the following:

    #!/usr/bin/env python3
    print "test"
    

    and do chmod +x test.py to make your script executable. After this when you do ./test.py you should get an error saying:

      File "./test.py", line 2
        print "test"
                   ^
    SyntaxError: Missing parentheses in call to 'print'
    

    because python3 doesn't supprt the print operator.

    Now go ahead and change the first line of your code to:

    #!/usr/bin/env python2
    

    and it'll work, printing test to stdout, because python2 supports the print operator. So, now you've learned how to switch between script interpreters.

    0 讨论(0)
  • 2020-11-21 06:55

    Technically, in Python, this is just a comment line.

    This line is only used if you run the py script from the shell (from the command line). This is know as the "Shebang!", and it is used in various situations, not just with Python scripts.

    Here, it instructs the shell to start a specific version of Python (to take care of the rest of the file.

    0 讨论(0)
  • 2020-11-21 06:55

    The line #!/bin/bash/python3 or #!/bin/bash/python specifies which python compiler to use. You might have multiple python versions installed. For example,
    a.py :

    #!/bin/bash/python3
    print("Hello World")
    

    is a python3 script, and
    b.py :

    #!/bin/bash/python
    print "Hello World"
    

    is a python 2.x script
    In order to run this file ./a.py or ./b.py is used, you need to give the files execution privileges before hand, otherwise executing will lead to Permission denied error.
    For giving execution permission,

    chmod +x a.py
    
    0 讨论(0)
  • 2020-11-21 06:56

    In order to run the python script, we need to tell the shell three things:

    1. That the file is a script
    2. Which interpreter we want to execute the script
    3. The path of said interpreter

    The shebang #! accomplishes (1.). The shebang begins with a # because the # character is a comment marker in many scripting languages. The contents of the shebang line are therefore automatically ignored by the interpreter.

    The env command accomplishes (2.) and (3.). To quote "grawity,"

    A common use of the env command is to launch interpreters, by making use of the fact that env will search $PATH for the command it is told to launch. Since the shebang line requires an absolute path to be specified, and since the location of various interpreters (perl, bash, python) may vary a lot, it is common to use:

    #!/usr/bin/env perl  instead of trying to guess whether it is /bin/perl, /usr/bin/perl, /usr/local/bin/perl, /usr/local/pkg/perl, /fileserver/usr/bin/perl, or /home/MrDaniel/usr/bin/perl on the user's system...

    On the other hand, env is almost always in /usr/bin/env. (Except in cases when it isn't; some systems might use /bin/env, but that's a fairly rare occassion and only happens on non-Linux systems.)

    0 讨论(0)
提交回复
热议问题