As Python gets updated older code will cease to run due to changes in Python syntax and functionality
To resolve this, we can run code in Containers but that adds a level of complexity
Another solution is to utilize pyenv and pipx and run the code in virtualized environments with the needed version of Python
Examples
Install and Run a Python App with a Specific Python Version
PrincipalMapper (pmapper) is a tool that requires Python version 3.9 to run (it errors out otherwise)
# install python 3.9.21pyenvinstall3.9.21# set version of python that pyenv will usepyenvglobal3.9.21# validate version is set (should match 3.9.21 for this example)python--version# install the tool, principalmapper (aka pmapper)pipxinstall--python $(pyenvwhichpython) principalmapper
Set up a Python Virtual Environment
We can set up a virtual environment to run any version of Python we want
This is useful for isolating your Python code/packages without impacting your system configuration
# install python 2.7.18pyenvinstall2.7.18# make a directory to work in mkdirvenv-tyler-python-2-7-18# create virtualenv for python 2.7.18pyenvvirtualenv2.7.18venv-tyler-python-2-7-18# change into the directorycdvenv-tyler-python-2-7-18# set directory for virtualenvpyenvlocalvenv-tyler-python-2-7-18# validate python version (should match 2.7.18)python--version
import jsonjson_dump ="./possible-subdomains.json"withopen(json_dump)as f: data = json.load(f)for host in data["results"]:print(host["host"], host["url"])# print out the host & URL
# install on linuxcurlhttps://pyenv.run|bash# add to ~/.zshrcecho'export PYENV_ROOT="$HOME/.pyenv"'>>~/.zshrcecho'[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"'>>~/.zshrcecho'eval "$(pyenv init - zsh)"'>>~/.zshrc# restart shell for PATH changes to take effectexec"$SHELL"# install python build dependencies (see install instructions as this is specific to your OS)# this is for Debian-based systems (Ubuntu, Kali, Parrot, etc.)sudoaptupdate; sudoaptinstallbuild-essentiallibssl-devzlib1g-dev \libbz2-dev libreadline-devlibsqlite3-devcurlgit \libncursesw5-dev xz-utilstk-devlibxml2-devlibxmlsec1-devlibffi-devliblzma-dev
Useful commands
# view all python versions installed with pyenvpyenvversions# set global (default) python version for pyenvpyenvglobal3.x.x# set local (directory) python version for pyenv (trumps global). Adds `.python-version` to directory. All pyenv commands in this directory will use this python versionpyenvlocal3.x.x# view available python versionspyenvinstall--list
Pipx
Installation
pipx is a useful tool for installing and running python code and apps in isolated virtual environments
# install on linuxsudoaptupdatesudoaptinstallpipxpipxensurepathsudopipxensurepath--global# optional to allow pipx actions with --global argument
Useful commands
# install package for user (~/.local/bin)pipxinstalltrufflehog# install package for all users (/usr/local/bin/)sudopipxinstalltrufflehog--global# view installed packagespipxlistsudopipxlist# uninstall packagespipxuninstalltrufflehogsudopipxuninstalltrufflehog--global