Python: main

Breaking down the Python from https://github.com/jorgebastida/awslogs

It’s installed with:

 

Files are packaged using a setup.py file. E.g.

https://github.com/jorgebastida/awslogs/blob/master/setup.py

https://packaging.python.org/tutorials/packaging-projects/

 

Here’s a sample script (/usr/local/bin/awslogs – source code: https://github.com/jorgebastida/awslogs):

 

#!/usr/local/opt/python@2/bin/python2.7

Path to Python. No idea why there’s an @.

https://stackoverflow.com/questions/53482939/why-is-there-an-symbol-in-the-shebang-of-my-python-script

Side note: you should use a shebang like:

#!/usr/bin/env python3

https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take

 

 

# -*- coding: utf-8 -*-

Specifies the encoding.

https://stackoverflow.com/questions/4872007/where-does-this-come-from-coding-utf-8

 

Imported as used later on. E.g. sys.argv[0]

 

from awslogs.bin import main

means: from the package awslogs.bin import the module main.

https://docs.python.org/2/tutorial/modules.html#more-on-modules

How does Python find these packages?

They’re in it’s path. E.g.

and

What is this dist-info directory?

It’s created by Wheel – one of Python’s packaging formats. See: https://packaging.python.org/discussions/wheel-vs-egg/

and

the mainfunction is from awslogs/bin.py here:

/usr/local/lib/python2.7/site-packages/awslogs/bin.py

More on sys.path: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

 

if __name__ == '__main__':

If run from the command line (e.g. python this-file.py ) then __name__ will == '__main__'. Otherwise, if it’s been run as a module then __name__ will be the name of the module.

https://stackoverflow.com/questions/419163/what-does-if-name-main-do

 

sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])

https://stackoverflow.com/questions/53483812/what-does-this-line-do-sys-argv0-re-subr-script-pyw-exe-s

https://www.pythonforbeginners.com/system/python-sys-argv

 

sys.exit(main())

https://stackoverflow.com/questions/5280203/what-does-this-mean-exit-main

 

Leave a Reply

Your email address will not be published. Required fields are marked *