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

前端 未结 12 1054
感动是毒
感动是毒 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:45

    Answer: Only if you plan to make it a command-line executable script.

    Here is the procedure:

    Start off by verifying the proper shebang string to use:

    which python
    

    Take the output from that and add it (with the shebang #!) in the first line.

    On my system it responds like so:

    $which python
    /usr/bin/python
    

    So your shebang will look like:

    #!/usr/bin/python
    

    After saving, it will still run as before since python will see that first line as a comment.

    python filename.py
    

    To make it a command, copy it to drop the .py extension.

    cp filename.py filename
    

    Tell the file system that this will be executable:

    chmod +x filename
    

    To test it, use:

    ./filename
    

    Best practice is to move it somewhere in your $PATH so all you need to type is the filename itself.

    sudo cp filename /usr/sbin
    

    That way it will work everywhere (without the ./ before the filename)

    0 讨论(0)
  • 2020-11-22 01:46

    You should add a shebang if the script is intended to be executable. You should also install the script with an installing software that modifies the shebang to something correct so it will work on the target platform. Examples of this is distutils and Distribute.

    0 讨论(0)
  • 2020-11-22 01:46

    The purpose of shebang is for the script to recognize the interpreter type when you want to execute the script from the shell. Mostly, and not always, you execute scripts by supplying the interpreter externally. Example usage: python-x.x script.py

    This will work even if you don't have a shebang declarator.

    Why first one is more "portable" is because, /usr/bin/env contains your PATH declaration which accounts for all the destinations where your system executables reside.

    NOTE: Tornado doesn't strictly use shebangs, and Django strictly doesn't. It varies with how you are executing your application's main function.

    ALSO: It doesn't vary with Python.

    0 讨论(0)
  • 2020-11-22 01:47

    Absolute vs Logical Path:

    This is really a question about whether the path to the Python interpreter should be absolute or Logical (/usr/bin/env) in respect to portability.

    Encountering other answers on this and other Stack sites which talked about the issue in a general way without supporting proofs, I've performed some really, REALLY, granular testing & analysis on this very question on the unix.stackexchange.com. Rather than paste that answer here, I'll point those interested to the comparative analysis to that answer:

    https://unix.stackexchange.com/a/566019/334294

    Being a Linux Engineer, my goal is always to provide the most suitable, optimized hosts for my developer clients, so the issue of Python environments was something I really needed a solid answer to. My view after the testing was that the logical path in the she-bang was the better of the (2) options.

    0 讨论(0)
  • 2020-11-22 01:50

    The shebang line in any script determines the script's ability to be executed like a standalone executable without typing python beforehand in the terminal or when double clicking it in a file manager (when configured properly). It isn't necessary but generally put there so when someone sees the file opened in an editor, they immediately know what they're looking at. However, which shebang line you use IS important.

    Correct usage for Python 3 scripts is:

    #!/usr/bin/env python3
    

    This defaults to version 3.latest. For Python 2.7.latest use python2 in place of python3.

    The following should NOT be used (except for the rare case that you are writing code which is compatible with both Python 2.x and 3.x):

    #!/usr/bin/env python
    

    The reason for these recommendations, given in PEP 394, is that python can refer either to python2 or python3 on different systems. It currently refers to python2 on most distributions, but that is likely to change at some point.

    Also, DO NOT Use:

    #!/usr/local/bin/python
    

    "python may be installed at /usr/bin/python or /bin/python in those cases, the above #! will fail."

    --"#!/usr/bin/env python" vs "#!/usr/local/bin/python"

    0 讨论(0)
  • 2020-11-22 01:54

    Use first

    which python
    

    This will give the output as the location where my python interpreter (binary) is present.

    This output could be any such as

    /usr/bin/python
    

    or

    /bin/python
    

    Now appropriately select the shebang line and use it.

    To generalize we can use:

    #!/usr/bin/env
    

    or

    #!/bin/env
    
    0 讨论(0)
提交回复
热议问题