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

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

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

21条回答
  •  醉话见心
    2020-11-21 07:02

    The main reason to do this is to make the script portable across operating system environments.

    For example under mingw, python scripts use :

    #!/c/python3k/python 
    

    and under GNU/Linux distribution it is either:

    #!/usr/local/bin/python 
    

    or

    #!/usr/bin/python
    

    and under the best commercial Unix sw/hw system of all (OS/X), it is:

    #!/Applications/MacPython 2.5/python
    

    or on FreeBSD:

    #!/usr/local/bin/python
    

    However all these differences can make the script portable across all by using:

    #!/usr/bin/env python
    

提交回复
热议问题