I have a Python package in a git repository. My code resides in the src/
folder.
I have a script, run.sh
, gitignored, that does the job for me of activating the virtualenv and running my package with the appropriate arguments.
My git repository makes heavy use of version tags. I want to be able to run a “stable” version, alongside the in-development version, without the git checkout hell.
I need to be able to run both stable and development versions in the same working directory, since they need to use the same data/
folder (also gitignored).
In order to achieve this, I need sys.path
to get changed.
Specifically, I need to remove the src/
directory from sys.path
(for the stable run).
For this purpose, I made a script run-stable.sh
that does the checkout and copies the source files to a gitignored location (let’s say, build/stable/
):
#!/usr/bin/bash
cd ~/path/to/mypackage && . venv/bin/activate || exit 1
latest_stable=0.4.1
build_ver=(`cat build/stable.version 2>/dev/null`)
if [[ "$build_ver" != "$latest_stable" ]]
then
! { git diff --quiet && git diff --cached --quiet; } && echo "uncommitted files, cannot build" >&2 && exit 1
! git show-ref --tags v"$latest_stable" -q && echo "tag $latest_stable not found" >&2 && exit 1
[[ ! -d "build/stable" ]] && mkdir -p build/stable
dev_commit=(`git rev-parse HEAD`)
build_commit=(`git show-ref --tags v"$latest_stable"`)
git checkout "$build_commit"
cp -af src/mypackage build/stable/mypackage
git checkout "$dev_commit" # <-- Issue here: When on master, VSCode does not detect it is on master after the build, but HEAD and master still coincide
echo "Build upgraded: $build_ver -> $latest_stable"
echo "$latest_stable" >build/stable.version
fi
# My current attempt to change sys.path (failed) ---v
cat <<<"import sys
sys.path.insert(0,'${PWD//'/\'}/build/stable')
sys.path.remove('${PWD//'/\'}/src')" >"$PWD"/build/stable.startup
PYTHONSTARTUP="$PWD"/build/stable.startup python3 -m mypackage "$@"
My attempts to manipulate sys.path
have had no effect as of now: sys.path
is still unchanged, and the development version is run instead.
I have tried setting PYTHONPATH
, PYTHONSTARTUP
(both with process substitution and an actual file created on the fly), but with no effect.
It seems the script specified on PYTHONSTARTUP
does not get to be run at all, no matter whether the build/stable/
path gets prepended, the src/
path gets removed, etc.
I want it to be done at bash level. When the Python package gets to be executed, it is too late. And I have quite complex handling in the __main__.py
file of my package.
My package has a pyproject.toml
. I installed my package with pip install -e
into my virtualenv, and I currently use the same virtualenv for both stable and development.