@@ -237,8 +237,14 @@ 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
240+ # - optionally update pyvenv.cfg to point to the portable Python directory (only at runtime)
241+ # Usage: _makeVenvPortable [--update-pyvenv-cfg]
241242_makeVenvPortable () {
243+ local update_pyvenv_cfg=false
244+ if [ " $1 " = " --update-pyvenv-cfg" ]; then
245+ update_pyvenv_cfg=true
246+ fi
247+
242248 local venv_dir=" ${EDIR} /venv"
243249 local vbin=" ${venv_dir} /bin"
244250
@@ -256,33 +262,35 @@ _makeVenvPortable() {
256262 ln -s " ${rel_py} " " ${vbin} /python3"
257263 ln -s " python3" " ${vbin} /python"
258264
259- # 2) Update pyvenv.cfg to point to the portable Python directory
265+ # 2) Update pyvenv.cfg to point to the portable Python directory (only at runtime)
260266 # 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} "
267+ if [ " $update_pyvenv_cfg " = " true" ]; then
268+ local pyvenv_cfg=" ${venv_dir} /pyvenv.cfg"
269+ if [ -f " ${pyvenv_cfg} " ]; then
270+ local portable_dir=" $( _portable_dir) "
271+ # Resolve to absolute path - this ensures it works when the backend is copied
272+ # Only resolve if the directory exists (it should if ensurePortablePython was called)
273+ if [ -d " ${portable_dir} " ]; then
274+ portable_dir=" $( cd " ${portable_dir} " && pwd) "
275+ else
276+ # Fallback to relative path if directory doesn't exist yet
277+ portable_dir=" ../python"
278+ fi
279+ local sed_i=(sed -i)
280+ # macOS/BSD sed needs a backup suffix; GNU sed doesn't. Make it portable:
281+ if sed --version > /dev/null 2>&1 ; then
282+ sed_i=(sed -i)
283+ else
284+ sed_i=(sed -i ' ' )
285+ fi
286+ # Update the home field in pyvenv.cfg
287+ # Handle both absolute paths (starting with /) and relative paths
288+ if grep -q " ^home = " " ${pyvenv_cfg} " ; then
289+ " ${sed_i[@]} " " s|^home = .*|home = ${portable_dir} |" " ${pyvenv_cfg} "
290+ else
291+ # If home field doesn't exist, add it
292+ echo " home = ${portable_dir} " >> " ${pyvenv_cfg} "
293+ fi
286294 fi
287295 fi
288296
@@ -347,19 +355,14 @@ function ensureVenv() {
347355 fi
348356 fi
349357 if [ " x${PORTABLE_PYTHON} " == " xtrue" ]; then
358+ # During install, only update symlinks and shebangs, not pyvenv.cfg
350359 _makeVenvPortable
351360 fi
352361 fi
353362
354363 # 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)
356364 if [ -x " $( _portable_python) " ]; then
357365 _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
363366 fi
364367
365368 if [ " x${VIRTUAL_ENV:- } " != " x${EDIR} /venv" ]; then
@@ -457,6 +460,11 @@ function installRequirements() {
457460# - ${BACKEND_NAME}.py
458461function startBackend() {
459462 ensureVenv
463+ # Update pyvenv.cfg before running to ensure paths are correct for current location
464+ # This is critical when the backend position is dynamic (e.g., copied from container)
465+ if [ " x${PORTABLE_PYTHON} " == " xtrue" ] || [ -x " $( _portable_python) " ]; then
466+ _makeVenvPortable --update-pyvenv-cfg
467+ fi
460468 if [ ! -z " ${BACKEND_FILE:- } " ]; then
461469 exec " ${EDIR} /venv/bin/python" " ${BACKEND_FILE} " " $@ "
462470 elif [ -e " ${MY_DIR} /server.py" ]; then
0 commit comments