Should I put #! (shebang) in Python scripts, and what form should it take?

前端 未结 12 1264
感动是毒
感动是毒 2020-11-22 01:27

Should I put the shebang in my Python scripts? In what form?

#!/usr/bin/env python 

or



        
12条回答
  •  面向向阳花
    2020-11-22 01:59

    If you have more than one version of Python and the script needs to run under a specific version, the she-bang can ensure the right one is used when the script is executed directly, for example:

    #!/usr/bin/python2.7
    

    Note the script could still be run via a complete Python command line, or via import, in which case the she-bang is ignored. But for scripts run directly, this is a decent reason to use the she-bang.

    #!/usr/bin/env python is generally the better approach, but this helps with special cases.

    Usually it would be better to establish a Python virtual environment, in which case the generic #!/usr/bin/env python would identify the correct instance of Python for the virtualenv.

提交回复
热议问题