I should put #! (Shebang) What form should it take in a Python script?

本文翻译自:Should I put #! (shebang) in Python scripts, and what form should it take?

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

#!/usr/bin/env python 

or either

#!/usr/local/bin/python

Are these equally portable? Are these equally portable? Which form is used most? Which form is used most ?

Note: the tornado project uses the shebang. Note: the tornado project uses shebang. At The Hand at The OTHER ON Django Project does not. On the other hand, Django project no.


#1st Floor

Reference: https://stackoom.com/question/Sz7f/ should I put -shebang- in a Python script-what form should it take


#2nd Floor

Sometimes, IF at The answer IS not Very the Clear (the I Mean you CAN not Decide IF yes or NO), the then IT does not Matter TOO much, and you CAN the ignore at The problem an until at The answer IS the Clear. Sometimes, if the answer is not very clear (I Means that you ca n’t decide yes or no), then it does n’t matter much, and until the answer is clear, you can ignore the question.

The #! only purpose is for launching the script. The only purpose is for launching the script . Django loads the sources on its own and uses them. Django will load and use the sources on its own . It never needs to decide what interpreter should be used . This way, the so, Actually Makes Sense NO here Wallpaper. Here there is practically no sense.#! #! #!

Generally, if it is a module and can not be used as a script, there is no need for using the #! usual, if it is a module and can not be used as a script, you do not need to use #! . . On the other hand, a module source often contains if __name__ == '__main__': ...with at least some trivial testing of the functionality. On the other hand, the module source usually contains if __name__ == '__main__': ..., and at least some trivial testing of the function . Then the #! then #! makes sense again. It makes sense again .

One good reason for using #! use #!a good reason #! is when you use both Python 2 and Python 3 scripts - they must be interpreted by different versions of Python. When is that when you use Python 2 and Python 3 script - they must be made Different versions of Python explained. Way the this, you have have the What to Remember pythonthe MUST BE Used at The Script Manually Launching the when (the without at The #!Inside). In this way, you have to remember what you must use a manual startup script python(no internal #!). If you have a mixture of such scripts , it is a good idea to use the #! If you mix these scripts, it is best to use #! inside, make them executable, and launch them as executables (chmod ...). Internally, Set it as an executable file and then start it as an executable file (chmod ...).

When using MS-Windows, the #! use of Windows-MS, #! HAD NO Sense - Recently Logs in an until. It does not make sense - until recently. Python 3.3 introduces a Windows Python Launcher (py.exe and pyw.exe) that reads the #! Python 3.3 introduces a Windows Python Launcher (py.exe and pyw.exe), which reads the #! line, detects the installed versions of Python, and uses the correct or explicitly wanted version of Python. OK, to detect the installed Python version and use the correct or explicitly required version of Python. As the extension can be associated with a program, you can get similar behaviour in Windows as with execute flag in Unix-based systems. Since the extension can be associated with a program, the execute flag in Unix-based systems can be obtained in Windows Similar behavior.


#3rd floor

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: If you have multiple versions of Python, And the script needs to run under a specific version, so when directly executing the script, she-bang can ensure that the correct version is used, 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. Please note that the script can still be run via the complete Python command line or via import, in this case , She-bang will be ignored. But for scripts run directly, this is a decent reason to use the she-bang. But for scripts that run directly, this is a good reason to use she-bang .

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

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


#4th floor

The shebang line in any script determines the script's ability to be executed like a standalone executable without typing pythonbeforehand in the terminal or when double clicking it in a file manager (when configured properly). The shebang line in any script determines that the script has The ability to execute like an independent executable file without having to enter it in the terminal in advance pythonor double-click it in the file manager (if the configuration is correct). 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. Not necessary, but usually put there, so when someone sees the file opened in the editor They will immediately know what they are looking at. HOWEVER, Which you use the shebang Line IS Important. However, the family silver line you are using is very important.

Correct usage for Python 3 scripts is: The correct usage of Python 3 scripts is:

#!/usr/bin/env python3

This defaults to version 3.latest. The default is version 3.latest. For Python 2.7.latest use python2in place of python3. For Python 2.7.latest, please use python2instead python3.

Following at The Should the NOT BE Used (the except for that you are at The Rare Case Writing code Which IS compatible with both-Python 2.x and 3.x): You should not use the following (except in rare cases, you're writing with Python 2 .x and 3.x compatible codes):

#!/usr/bin/env python

The reason for these recommendations, given in PEP 394 , is that pythoncan refer either to python2or python3on different systems. The reason for giving these recommendations in PEP 394 is that they pythoncan be referenced python2or on different systems python3. It currently refers to python2on most distributions, but that is likely to change at some point. Currently, it is used in most distributions , but this may change at some point .python2

Also, DO NOT Use: In addition , 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." "In this case, python may be installed at / usr / bin / python or / bin / On Python, the above #! will fail. "

-- "#!/usr/bin/env python" vs "#!/usr/local/bin/python" - “#!/ usr / bin / env python”与“#!/ usr / local / bin / python”


#5th Floor

Should I put the shebang in my Python scripts? Should I put the shebang in my Python scripts ?

Put a shebang into a Python script to indicate: Put a shebang into a Python script to indicate :

  • this module can be run as a script of the module can be run as a script
  • Whether it can be run only on python2, python3 or is it Python 2/3 compatible is it only run on python2, python3 or is it compatible with Python 2/3?
  • on POSIX, it is necessary if you want to run the script directly without invoking pythonexecutable explicitly on POSIX, if you want to run the script directly without explicitly calling the pythonexecutable file, it is necessary

Are these equally portable? Are these equally portable? Which form is used most? Which form is used most ?

The shebang you the Write A IF Manually the then Always use #!/usr/bin/env pythonThe unless you have have A specific reason not to use IT. If you manually write the shebang , please always use #!/usr/bin/env pythonunless there is a specific reason not to use it. This form is understood even on Windows (Python launcher). This form can be understood even on Windows (Python launcher) .

Note: Installed scripts Should use python A specific Executable EG, /usr/bin/pythonor /home/me/.virtualenvs/project/bin/python. Note: the installed scripts should use a particular python executable files, for example, /usr/bin/pythonor /home/me/.virtualenvs/project/bin/python. It is bad if some tool breaks if you activate a virtualenv in your shell. If you activate virtualenv in Shell, if some tools are broken, it's bad. Luckily, the correct shebang is created automatically in most cases by setuptoolsor your distribution package tools (on Windows, setuptoolscan generate wrapper .exescripts automatically). Fortunately, in most cases, the setuptoolscorrect shebang (in On Windows, setuptoolswrapper .exescripts can be generated automatically ).

In other words, if the script is in a source checkout then you will probably see #!/usr/bin/env python. In other words, if the script is in source checkout , then you might see it #!/usr/bin/env python. If it is installed then the shebang is a path to a specific python executable such as #!/usr/local/bin/python(NOTE: you should not write the paths from the latter category manually). If it is installed, then shebang is the path of the specific python executable file, for example #!/usr/local/bin/python( Note: You should not manually write paths from the latter category).

The To the Choose model types within you Should use python, python2, or python3in at The shebang, See PEP 394 - at The "Python" the Command ON Unix-Like Systems : To select whether to use shebang in python, python2or python3, please refer to the PEP 394-Unix-like Systems " "python" command :

  • ... pythonShould BE Used in Line at The shebang only for scripts that are compatible with both-Source Python 2 and 3. ... pythonshould only be used in the shebang line in the script is compatible with Python 2 and 3 sources.

  • in preparation for an eventual change in the default version of Python, Python 2 only scripts should either be updated to be source compatible with Python 3 or else to use python2in the shebang line. In preparation for the final change of the default version of Python, only Python The script for 2 was updated to be compatible with Python 3 sources, or used in the shebang line python2.


#6th floor

Use first the first to use

which python

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

This output could be any such as this may be any such output

/usr/bin/python

or either

/bin/python

Now appropriately select the shebang line and use it .

To generalize we can use: To summarize, we can use:

#!/usr/bin/env

or either

#!/bin/env
Published 0 original articles · praised 75 · 560,000 views +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105468319