Skip to content

Commit 4185199

Browse files
committed
fix(python): set pyvenv python home
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent c25b305 commit 4185199

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

backend/python/common/libbackend.sh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ function getBuildProfile() {
237237
# Make the venv relocatable:
238238
# - rewrite venv/bin/python{,3} to relative symlinks into $(_portable_dir)
239239
# - normalize entrypoint shebangs to /usr/bin/env python3
240+
# - update pyvenv.cfg to point to the portable Python directory
240241
_makeVenvPortable() {
241242
local venv_dir="${EDIR}/venv"
242243
local vbin="${venv_dir}/bin"
@@ -255,7 +256,37 @@ _makeVenvPortable() {
255256
ln -s "${rel_py}" "${vbin}/python3"
256257
ln -s "python3" "${vbin}/python"
257258

258-
# 2) Rewrite shebangs of entry points to use env, so the venv is relocatable
259+
# 2) Update pyvenv.cfg to point to the portable Python directory
260+
# Use absolute path resolved at runtime so it works when the venv is copied
261+
local pyvenv_cfg="${venv_dir}/pyvenv.cfg"
262+
if [ -f "${pyvenv_cfg}" ]; then
263+
local portable_dir="$(_portable_dir)"
264+
# Resolve to absolute path - this ensures it works when the backend is copied
265+
# Only resolve if the directory exists (it should if ensurePortablePython was called)
266+
if [ -d "${portable_dir}" ]; then
267+
portable_dir="$(cd "${portable_dir}" && pwd)"
268+
else
269+
# Fallback to relative path if directory doesn't exist yet
270+
portable_dir="../python"
271+
fi
272+
local sed_i=(sed -i)
273+
# macOS/BSD sed needs a backup suffix; GNU sed doesn't. Make it portable:
274+
if sed --version >/dev/null 2>&1; then
275+
sed_i=(sed -i)
276+
else
277+
sed_i=(sed -i '')
278+
fi
279+
# Update the home field in pyvenv.cfg
280+
# Handle both absolute paths (starting with /) and relative paths
281+
if grep -q "^home = " "${pyvenv_cfg}"; then
282+
"${sed_i[@]}" "s|^home = .*|home = ${portable_dir}|" "${pyvenv_cfg}"
283+
else
284+
# If home field doesn't exist, add it
285+
echo "home = ${portable_dir}" >> "${pyvenv_cfg}"
286+
fi
287+
fi
288+
289+
# 3) Rewrite shebangs of entry points to use env, so the venv is relocatable
259290
# Only touch text files that start with #! and reference the current venv.
260291
local ve_abs="${vbin}/python"
261292
local sed_i=(sed -i)
@@ -321,8 +352,14 @@ function ensureVenv() {
321352
fi
322353

323354
# We call it here to make sure that when we source a venv we can still use python as expected
355+
# Also make existing venvs portable if we're using portable Python (e.g., after copying from container)
324356
if [ -x "$(_portable_python)" ]; then
325357
_macosPortableEnv
358+
# If venv exists and we're using portable Python, ensure it's made portable
359+
# This handles the case where the venv was copied from a container
360+
if [ -d "${EDIR}/venv" ] && [ "x${PORTABLE_PYTHON}" == "xtrue" ]; then
361+
_makeVenvPortable
362+
fi
326363
fi
327364

328365
if [ "x${VIRTUAL_ENV:-}" != "x${EDIR}/venv" ]; then

0 commit comments

Comments
 (0)