Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python C API: Modify search path

Tags:

python

c

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?

like image 916
Paul Manta Avatar asked Aug 21 '11 09:08

Paul Manta


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.

like image 108
Ignacio Vazquez-Abrams Avatar answered Sep 25 '22 05:09

Ignacio Vazquez-Abrams


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"
);
like image 25
mloskot Avatar answered Sep 26 '22 05:09

mloskot