How can I add a speciific directory to the search path using the C API? And a related question: will the changes be local to the application, or is the search path global?
2 Answers
Use PySys_GetObject("path") to retrieve sys.path, then manipulate it as you would any other sequence or list. Changes will be local to the Python interpreter/VM.
3 Comments
Jiminion
Why isn't there a PySys_GetPath() in the API?
Alan
There is a PySys_SetPath(const wchar_t *path). docs.python.org/3/c-api/sys.html
ChrCury78
PySys_SetPath is deprecated since version 3.11.
You can update search path using the well-known Python code but called from within your C module:
PyRun_SimpleString(
"import sys\n"
"sys.path.append('/your/custom/path/here')\n"
);