From 7c7b6ce146e4c2d9bbc092016fbc5e40488bbb50 Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Thu, 6 Jun 2024 10:09:45 -0500 Subject: [PATCH] Base: Support virtual environment in Py>=3.11 --- src/Base/Interpreter.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Base/Interpreter.cpp b/src/Base/Interpreter.cpp index 2f84e0cb14..2bdc54ccff 100644 --- a/src/Base/Interpreter.cpp +++ b/src/Base/Interpreter.cpp @@ -605,22 +605,22 @@ void initInterpreter(int argc, char* argv[]) throw Base::RuntimeError("Failed to init from config"); } + // If FreeCAD was run from within a Python virtual environment, ensure that the site-packages + // directory from that environment is used. + const char* virtualenv = getenv("VIRTUAL_ENV"); + if (virtualenv) { + std::wstringstream ss; + PyConfig_Read(&config); + ss << virtualenv << L"/lib/python" << PY_MAJOR_VERSION << "." << PY_MINOR_VERSION + << "/site-packages"; + PyObject* venvLocation = PyUnicode_FromWideChar(ss.str().c_str(), ss.str().size()); + PyObject* path = PySys_GetObject("path"); + PyList_Append(path, venvLocation); + } + PyConfig_Clear(&config); Py_Initialize(); - const char* virtualenv = getenv("VIRTUAL_ENV"); - if (virtualenv) { - PyRun_SimpleString( - "# Check for virtualenv, and activate if present.\n" - "# See " - "https://virtualenv.pypa.io/en/latest/userguide/#using-virtualenv-without-bin-python\n" - "import os\n" - "import sys\n" - "base_path = os.getenv(\"VIRTUAL_ENV\")\n" - "if not base_path is None:\n" - " activate_this = os.path.join(base_path, \"bin\", \"activate_this.py\")\n" - " exec(open(activate_this).read(), {'__file__':activate_this})\n"); - } } } // namespace const char* InterpreterSingleton::init(int argc, char* argv[])