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?
Asked
Active
Viewed 5,139 times
2 Answers
15
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.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358
-
1Why isn't there a PySys_GetPath() in the API? – Jiminion Jul 25 '18 at 20:32
-
There is a PySys_SetPath(const wchar_t *path). https://docs.python.org/3/c-api/sys.html – Alan Sep 16 '20 at 19:22
-
PySys_SetPath is deprecated since version 3.11. – ChrCury78 May 16 '23 at 22:35
15
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"
);