Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Python library modules start with #!/usr/bin/env python?

Should Python library modules start with #!/usr/bin/env python?

Looking at first lines of *.py in /usr/share/pyshared (where Python libs are stored in Debian) reveals that there are both files that start with the hashbang line and those that do not.

Is there a reason to include or omit this line?

like image 284
Mischa Arefiev Avatar asked Mar 20 '12 08:03

Mischa Arefiev


People also ask

Do you still need __ init __ py?

__init__.py file is not needed from python version 3.3 and above !! All folders from python version 3.3 and above are considered a namespace package. All folders from python 3.3 and above having a __init__.py file are considers a standard package.

Which Python library should I learn first?

There are a group of core libraries you'll need to learn. Pandas should be first. Everything you do is data centric. Next, NumPy… then SciKit-Learn, Matplotlib.

Do Python modules need an init?

Although Python works without an __init__.py file you should still include one. It specifies that the directory should be treated as a package, so therefore include it (even if it is empty).

Do Python modules need to be in the same folder?

No, files can be imported from different directories.


1 Answers

The reason why some files in /usr/share/pyshared have declared the shebang & some do not are easy to explain. Take the files uno.py and pyinotify.py. The former has no shebang and the latter has.

  1. 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.
  2. 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()
    

You can hardcode the python binary in the shebang, but as others have mentioned, using /usr/bin/env will make it more portable.

like image 154
canadadry Avatar answered Oct 11 '22 05:10

canadadry