Skip to content

Instantly share code, notes, and snippets.

@tsibley
Last active March 20, 2023 22:20
Show Gist options
  • Select an option

  • Save tsibley/ffb6469067b1cf8976be227d52548749 to your computer and use it in GitHub Desktop.

Select an option

Save tsibley/ffb6469067b1cf8976be227d52548749 to your computer and use it in GitHub Desktop.
#!/bin/bash
# usage: pypreload <code> <cmd> [<arg> [<arg> […]]]
#
# See <https://docs.python.org/3/library/site.html> for how the "sitecustomize"
# module works.
#
set -euo pipefail
code="$1"
shift
tmplib="$(mktemp -dt "$(basename "$0")"-XXXXXX)"
cat > "$tmplib/sitecustomize.py" <<~~
# written by $0 to run: $*
$code
# remove ourselves from consideration
import sys, os.path
del sys.modules["sitecustomize"]
sys.path.remove(os.path.dirname(__file__))
# import any sitecustomize we masked
import sitecustomize
~~
PYTHONPATH="$tmplib${PYTHONPATH:+:}${PYTHONPATH:-}"
export PYTHONPATH
exec "$@"
@tsibley

tsibley commented Mar 20, 2023

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment