Breaking down the Python from https://github.com/jorgebastida/awslogs
It’s installed with:
1 |
pip install awslogs --ignore-installed six |
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):
1 2 3 4 5 6 7 8 9 10 11 |
#!/usr/local/opt/python@2/bin/python2.7 # -*- coding: utf-8 -*- import re import sys from awslogs.bin import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit(main()) |
#!/usr/local/opt/python@2/bin/python2.7
Path to Python. No idea why there’s an @
.
Side note: you should use a shebang like:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
Specifies the encoding.
https://stackoverflow.com/questions/4872007/where-does-this-come-from-coding-utf-8
1 2 |
import re import sys |
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.
1 2 3 |
python import sys print '\n'.join(sys.path) |
and
1 2 3 4 5 6 7 8 |
ls /usr/local/lib/python2.7/site-packages/awslogs* /usr/local/lib/python2.7/site-packages/awslogs: __init__.py _version.py bin.py core.py exceptions.py __init__.pyc _version.pyc bin.pyc core.pyc exceptions.pyc /usr/local/lib/python2.7/site-packages/awslogs-0.10.0.dist-info: DESCRIPTION.rst METADATA WHEEL metadata.json top_level.txt INSTALLER RECORD entry_points.txt pbr.json |
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 main
function 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://www.pythonforbeginners.com/system/python-sys-argv
sys.exit(main())
https://stackoverflow.com/questions/5280203/what-does-this-mean-exit-main