Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override Py_GetPrefix(), Py_GetPath()?

Tags:

c++

python

api

I'm trying to embed the Python interpreter and need to customize the way the Python standard library is loaded. Our library will be loaded from the same directory as the executable, not from prefix/lib/.

We have been successful in making this work by manually modifying sys.path after calling Py_Initialize(), however, this generates a warning because Py_Initialize is looking for site.py in ./lib/, and it's not present until after Py_Initialize has been called and we have updated sys.path.

The Python c-api docs hint that it's possible to override Py_GetPrefix() and Py_GetPath(), but give no indication of how. Does anyone know how I would go about overriding them?

like image 479
Jeremy Cowles Avatar asked Jul 17 '09 22:07

Jeremy Cowles


2 Answers

You could set Py_NoSiteFlag = 1, call PyInitialize and import site.py yourself as needed.

like image 80
ars Avatar answered Sep 21 '22 01:09

ars


Have you considered using putenv to adjust PYTHONPATH before calling Py_Initialize?

like image 34
brendan Avatar answered Sep 20 '22 01:09

brendan