pip-mod: install Python packages to module

Post tutorials, HOWTO's and other useful resources here.
pterid
Contributor
Contributor
Posts: 105
Joined: 01 Feb 2025, 20:13
Distribution: Porteus 5.01 Xfce on ext4 USB

pip-mod: install Python packages to module

Post#1 by pterid » 19 Mar 2026, 10:48

Like most Linux distros, Porteus comes with system Python installed. Python has its own package manager (pip) and a huge library of packages at Pypi. (the packages' quality varies; don't run arbitrary code on your system without forethought...)

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
Some are available as Slackware packages; in those cases, just get them in the normal way, they will be more likely to work seamlessly. But for some packages that is not an option.

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
Have fun and reply with any bugs you find with the script or the idea generally please!

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

User avatar
dreadbird
Shogun
Shogun
Posts: 251
Joined: 08 Dec 2024, 04:30
Distribution: porteus5

pip-mod: install Python packages to module

Post#2 by dreadbird » 27 Mar 2026, 16:07

pterid wrote:
19 Mar 2026, 10:48
Like most Linux distros, Porteus comes with system Python installed. Python has its own package manager (pip) and a huge library of packages at Pypi. (the packages' quality varies; don't run arbitrary code on your system without forethought...)

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
Some are available as Slackware packages; in those cases, just get them in the normal way, they will be more likely to work seamlessly. But for some packages that is not an option.

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
Have fun and reply with any bugs you find with the script or the idea generally please!

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
will try it out keeps things organized I like it

Post Reply