Sometimes you need to install extra packages into your system Python to get certain software to run, or just to get something useful to happen. I had three examples recently:
- to debug my Chromebook's trackpad with libinput measure touchpad-pressure I needed to install Python packages libevdev and pyudev
- to catch syntax errors in my work on the Gtk slapt-mod wrapper, I installed pylint (which can be used as a command line tool outside the Python interpreter - very useful)
- a tool to connect to a university wifi system required dbus-python
You can get pip and then just pip install the Python packages, but the files will be installed in your persistent changes, which you might not want if the packages are things you won't always need. You can also grab the changes fairly cleanly via changes-time in an Always Fresh environment, but that involves two reboots and some curating of the stuff you dump. And a load of __pycache__ files end up in the module, which are not strictly necessary (they contained compiled code, but you can have Python recompile later when it uses the module). So here's one way you can make a module of some Python packages quickly.
- if you don't have pip already, install it via sudo slapt-mod -m python-pip (and activate it of course...)
- save the script below (or something like it) as pip-mod in /usr/local/bin or /usr/bin folder (and don't forget to chmod +x pip-mod)
- know the name of the pip install ... package you need, by visiting pypi.org (there is no way to list available packages on the command line, sorry)
- run sudo pip-mod package1 package2 ... (all the packages and their dependencies will be made into one module in /tmp)
- copy the module from /tmp to your modules folder
Code: Select all
# pip-mod
# run as root
script_name="$(basename "$0")"
if [ "$#" -eq 0 ]; then
echo "Usage: $script_name package1 package2 ... " >&2
exit
fi
if ! which pip &>/dev/null; then
echo "$script_name: could not find pip!"
exit 1
fi
if [ "$(whoami)" != "root" ]; then
echo "$script_name: Please run as root."
exit 1
fi
TMP="/tmp"
PIPMODDIR="${TMP}/PIPMOD"
[ -e "$PIPMODDIR" ] && rm -r "$PIPMODDIR"
mkdir -p "$PIPMODDIR"
pip install --root "$PIPMODDIR" "$@"
# optional - remove pycache files
# if you set environment variable PYTHONDONTWRITEBYTECODE=true
# then Python will not store them in your changes either
find "$PIPMODDIR" -type d -name '__pycache__' | xargs rm -r
mod_name="python-$(echo "$*" | tr ' ' '-').xzm"
mod_path="$TMP/${mod_name}"
if [ -d "$PIPMODDIR/usr" ]; then
dir2xzm "$PIPMODDIR" "$mod_path"
else
echo "$script_name: Failed to install Python packages to " >&2
exit 1
fi
if [ -f "$mod_path" ]; then
echo "$script_name: Created module at ${mod_path}." >&2
else
echo "$script_name: Failed to create module."
exit 1
fi






