8

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?

Puppy
  • 144,682
  • 38
  • 256
  • 465
Paul Manta
  • 30,618
  • 31
  • 128
  • 208

2 Answers2

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
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"
);
Community
  • 1
  • 1
mloskot
  • 37,086
  • 11
  • 109
  • 136