NOT FULLY PORTED YET.
Python is a popular and easy to use general purpose programming language that is heavily used in Data Analytics and Data Science as well as systems administration.
It's not as amazing for one-liners as Perl is though, which can boost shell scripts more easily.
- Core Reading
- DevOps Python tools
- Shell scripts with Python
- Nagios Plugins in Python
- Python Library with Unit Tests
- VirtualEnv
- Pipenv
- Jupyter Notebook
- Libraries
- Jython
- Troubleshooting
HariSekhon/DevOps-Python-tools
Shell scripts using Python and making it easier to install Python pip libraries from PyPI.
Creates a virtual environment in the local given sub-directory in which to install PyPI modules to avoid clashes with system python libraries.
virtualenv "$directory_name_to_create"
I like top always the directory name venv
for the virtualenv:
virtualenv venv
Then to use it before you starting pip
installing:
source venv/bin/activate
This prepends to $PATH
to use the bin/python
and lib/python-3.12/site-packages
under the local venv
directory:
Now install PyPI modules as usual.
The venv/pyvenv.cfg
file will contain some metadata like this:
home = /opt/homebrew/Cellar/[email protected]/3.12.3/bin
implementation = CPython
version_info = 3.12.3.final.0
virtualenv = 20.25.3
include-system-site-packages = false
base-prefix = /opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12
base-exec-prefix = /opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12
base-executable = /opt/homebrew/Cellar/[email protected]/3.12.3/Frameworks/Python.framework/Versions/3.12/bin/python3.12
https://pipenv.pypa.io/en/latest/
https://github.com/pypa/pipenv
Combines Pip and VirtualEnv into one command.
brew install pipenv
Creates a Pipfile
and Pipfile.lock
,
plus a virtualenv in a standard location $HOME/.local/share/virtualenvs/
if not already inside one.
pipenv install
Activates the virtualenv
pipenv shell
Automatically converts a requirements.txt
file into a Pipfile
:
pipenv check
Dependency graph:
pipenv graph
(formerly called IPython Notebook)
https://ipython.org/notebook.html
Interactive web page where you can mix code blocks, rich notes and graphs on the same page, click to execute code blocks and form a page oriented workflow of results and analysis for sharing and demonstrating.
You can see these used throughout these GitHub repos:
- HariSekhon/DevOps-Python-tools
- HariSekhon/Nagios-Plugins
- HariSekhon/DevOps-Bash-tools
- HariSekhon/pylib
GitPython
- Gitsh
- execute shell commands more easilyjinja2
- Jinja2 templatinghumanize
- converts units to human readablepyobjc-framework-Quartz
- control Mac UIpsutil
PyInstaller
- bundle Python code into standalone executablers (doesn't work for advanced code)sasl
requests
- easy HTTP request librarybeautifulsoup4
- HTML parsing library- Scrapy - web scraping
selenium
- Selenium web testing framework
mysqlclient
- MySQL clientpsycopg2
- PostgreSQLpsycopg2-binary
boto3
- AWSaws-consoler
python-jenkins
- JenkinsTravisPy
- for Travis CIpylint
- Python linting CLI toolgrip
- Grip renders local markdown using a local webserverMarkdown
MarkupSafe
checkov
semgrep
- security / misconfiguration scanningjsonlint
yamllint
- CLI YAML linting tool
unittest2
nose
- Faker - generate fake but realistic data for unit testing, Python version of the
original Perl library,
comes with a
faker
command convenient for shell scripts:
Generate 10 fake addresses:
faker -r 10 address
docker
- control local Dockerkubernetes
- Kubernetespyvmomi
- VMware
elasticsearch
- Elasticsearchhappybase
- HBaseimpyla
- Impalakazoo
- ZooKeeperPyHive
- for Apache Hivepython-krbV
- Kerberos support, often pulled as a dependency forsnakebite[kerberos]
snakebite
- HDFS
avro
- Avroldif3
- LDAP LDIF formatjsonlint
Markdown
MarkupSafe
numpy
- NumPy for scientific numeric processingpandas
- Pandas for data analysispython-cson
pyarrow
- Apache Arrow and Parquet support, but Parquet support in this is weak, prefer Parquet Toolspython-ldap
python-snappy
- work with Snappy compression format, often pulled as a dependencyPyYAML
- work with YAML files in Pythonsciki-learn
- SciKit Learntoml
xmltodict
yamllint
- CLI YAML linting tool
- Faker - generate fake but realistic data for unit testing, Python version of the
original Perl library,
comes with a
faker
command convenient for shell scripts:
Generate 10 fake addresses:
faker -r 10 address
matplotlib
- General-purpose plotting, highly customizableseaborn
- built onmatplotlib
, higher level to make it easier to great aesthetic visualizationsplotly
- Interactive graphs, dashboards, 3D plotsbokeh
- Interactive, web-ready visualizationspandas
- Quick and easy plots directly from dataframesnetworkx
- Graph theory, network analysisaltair
- Declarative statistical visualizationspygal
- Vector (SVG) visualizations, interactivegraph-tool
- Scalable and efficient for large graph analysis
Python on the Java JVM.
The ease of Python coding with full access to Java APIs and libraries.
Useful when there aren't Python libraries available or they aren't as fully featured as the Java versions (eg. for Hadoop).
Today, I'd prefer to write in the native JVM language Groovy.
From DevOps-Python-tools:
jython_install.sh
Interactive REPL:
$ jython
Jython 2.7.3 (tags/v2.7.3:5f29801fe, Sep 10 2022, 18:52:49)
[OpenJDK 64-Bit Server VM (Eclipse Adoptium)] on java17.0.1
Type "help", "copyright", "credits" or "license" for more information.
>>>
Run a Jython script and add Java classpath to find any jar dependencies that the script uses:
jython -J-cp "$CLASSPATH" "file.py"
Some Jython programs, such as those using Hadoop HDFS Java API can be found in the DevOps-Python-tools repo.
Prints stack trace on crash.
Useful for debugging native-level crashes, C extensions, system calls, OS signals.
Activates handling of signals like:
Signal | Description |
---|---|
SIGSEGV |
Segmentation fault |
SIGFPE |
Floating-point exception |
SIGBUS |
Bus error |
SIGABRT |
Abort signal |
SIGILL |
Illegal instruction |
Normally, these signals cause Python to crash without much useful information, but with the fault handler enabled, it'll output a traceback before the crash to help debug.
Minimal performance overhead, but bigger logs, and possibly dumps sensitive info.
export PYTHONFAULTHANDLER=1
or
python -X faulthandler "file.py"
or
import faulthandler
faulthandler.enable()
ModuleNotFoundError: No module named 'pip._vendor.six.moves'
Fix:
apk del py3-pip py-pip
apk add py3-pip
Partial port from private Knowledge Base page 2008+