9

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 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why isn't there a PySys_GetPath() in the API?
There is a PySys_SetPath(const wchar_t *path). docs.python.org/3/c-api/sys.html
PySys_SetPath is deprecated since version 3.11.
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"
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.