Fix virtualenv support #138
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This should hopefully solve #80, and perhaps other similar issues lurking about.
The discussion in #80 (comment) is spot on:
However, the Python executable for a virtualenv is determined not through the
PYTHONEXECUTABLE
environment variable, but throughargv[0]
. Here's the relevant code (all from v3.11.6, the version bundled in oss-cad-suite at the time of writing):base_executable
to to the value ofexecutable
, which was computed a bit higher up fromargv[0]
, and 2. setsexecutable
to the value ofPYTHONEXECUTABLE
.base_executable
is written back to the config dict.sys._base_executable
is set from the configuration here: https://github.com/python/cpython/blob/v3.11.6/Python/sysmodule.c#L3106.sys._base_executable
: https://github.com/python/cpython/blob/v3.11.6/Lib/venv/__init__.py#L130bin
directory: https://github.com/python/cpython/blob/v3.11.6/Lib/venv/__init__.py#L292The end result is that for oss-cad-suite, the venv is now subtly broken:
python3.11
is a symlink tolibexec/python3.11
, instead ofpy3bin/python3
orbin/tabbypy3
. And since the interpreter is dynamically linked tolibpython3.11.so
:Whoops, now running it will load the
.so
in/lib
instead of the one inoss-cad-suite/lib
, since there's no wrapper script to set the library path anymore.The fix, it seems, is simple -- override
argv[0]
instead of settingPYTHONEXECUTABLE
. Luckilyld.so
has an--argv0
flag just for this purpose. The first commit in this PR does precisely that.The second commit resolves any potential symlinks in the wrapper script's path. When called from a venv, the wrapper script will have
BASH_SOURCE
set tovenv/bin/python3
(for example), but we need to find the actual location where oss-cad-suite resides.On my Debian 12 system, cocotb in a virtualenv with iverilog now work just fine.