Python库模块是否应该以#!/usr/bin/env
python开头?
查看Debian中存储Python库的/usr/share/pyshared
中*.py
文件的第一行,发现有些文件以hashbang行开头,而有些则没有。
包含或省略此行的原因是什么?
Python库模块是否应该以#!/usr/bin/env
python开头?
查看Debian中存储Python库的/usr/share/pyshared
中*.py
文件的第一行,发现有些文件以hashbang行开头,而有些则没有。
包含或省略此行的原因是什么?
为什么在/usr/share/pyshared
文件夹中有些文件声明了shebang,而有些则没有?这很容易解释。以uno.py
和pyinotify.py
这两个文件为例,前者没有shebang,后者有。
uno.py
is a python module which will be imported and used in other programs/scripts. Thus it will never be executed directly from the command line. On the other hand pyinotify.py
contains the shebang and you can see that it contains the following line at the bottom (it can made into an executable if you run a chmod u+x
on it):
if __name__ == '__main__':
command_line()
您可以在shebang中硬编码Python二进制文件,但正如其他人所提到的那样,使用/usr/bin/env
将使其更具可移植性。
#!
行。大多数模块没有这样的功能,因此不需要这行代码。
#!python
有什么问题吗? - Kos/usr/bin/env
仅用于可移植性。 - Mischa Arefiev