Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: manually set sys.prefix, exec_prefix and sys.path
When activating a venv in the shell, sys.base_prefix and sys.base_exec_prefix should be set to the installation location while sys.prefix and sys.exec_prefix should be set to the venv dir. Unfortunately, when initialize_venv() and init_pyo3() are called, we can't use Py_InitializeFromConfig() to set any of these and certain platforms are unable to find python-build-standalone. For now, we'll temporarily set PYTHONHOME in init_pyo3() to the installation location to make python work. By setting it at this point in the code, sys.prefix and sys.exec_prefix end up also being set to the installation location, which is fine when not under a venv, but is different from when entering an venv. To address this, in PYTHON_INIT.call_once() and when VIRTUAL_ENV is set (which initialize_venv() will have set at this point), manually set sys.prefix and sys.exec_prefix to what is in VIRTUAL_ENV. Similarly, when activating a venv in the shell, sys.path is appended to have the venv's site-packages dir. Previously we were setting PYTHONPATH in initialize_venv() which ensures that the venv's site-packages dir is in sys.path, but this ends up having the venv's site-packages dir first in sys.path. To correct this, don't set PYTHONPATH any more and instead adjust PYTHON_INIT.call_once() to append the venv's site-packages dir to sys.path when VIRTUAL_ENV is set. Finally, when exiting init_pyo3(), unconditionally unset PYTHONHOME when VIRTUAL_ENV is set (like activation scripts do) and restore/unset when it isn't. Prior to these changes, all target incorrectly had the venv's site-packages first in sys.path and OSX and Windows additionally had an incorrect sys.prefix and sys.exec_prefix. With these initialization changes in place, the runtime environment for the plugins is much closer to that of a shell activated venv.
- Loading branch information