Add support for defining the Python installation dir under embedding mode

Issue #344 new
Anonymous created an issue

I'm porting a boost::python wrapper library to CFFI-based embedding, so I can compile the module for different Python interpreters (CPython and PyPy) without touching my code. CFFI does the job pretty well, but I need to set the python home directory manually before starting Python, this means calling: Py_SetPythonHome() for CPython, and pypy_setup_home() for PyPy. Would it be possible to add support for setting this parameter under the embedding mode? Thanks in advance!

Comments (3)

  1. Ezequiel Ruiz

    Actually yes, in some cases. Imagine you're working on an application that uses Python internally in some modules; To distribute your application, you must also distribute the Python library as well. Especially if running the application in a place that may have more than one python installation, that's why those functions are provided, instead of using the PTYHONHOME or PYTHONPATH environment variables, you can use those functions to indicate where to load the python library from. In fact, the only situation where I find Py_SetPythonHome() or pypy_setup_home() useful is when embedding the interpreter (even with CFFI). My current workaround is to patch the C code generated by CFFI with a harcoded PythonHome using a relative path where my application distributes the Python library. and manually building the DLL. It is perhaps a rare feature, but in my case it could help me a lot setting up my CFFI-made DLLs for the distributed applications, becasue I can define the relative location of the PythonLib at compile time.

    I would send you a patch for the Py_SetPythonHome() wrapper, but I have no idea of where and how to call pypy_setup_home() in CFFI. Also, I can't imagine the best way to implement this as an easy and undestandable function for the cffi builder interface!

    Thanks in advance for you help! And thank you for this awesome project, I really love it!

  2. Armin Rigo

    With PyPy, I'll maintain that any usage of pypy_setup_home() is deprecated. In fact the initialization code for cffi internally calls that function already, passing it as argument the path to the with which that code executes. So you should just include together with the Python standard library in directories lib-python and lib_pypy; as long as they are together, this will load these lib-python and lib_pypy automatically.

    I'm not sure about the case of CPython. I see that the problem is that there is no automatic link between the location of the and the standard library that it uses, and Py_SetPythonHome() might have been designed for that use case. Then you're right, we might need a way to call Py_SetPythonHome().

    Maybe CFFI for embedding on CPython should (optionally?) behave like PyPy: after we load the from the CFFI module, we look in nearby locations for a lib/pythonX.Y/ directory, and if we find it, we call Py_SetPythonHome() on it.

  3. Log in to comment