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

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

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

21条回答
  •  无人及你
    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
    

提交回复
热议问题