PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.03k stars 742 forks source link

Could there be an os independent wrapper around things like Py_SetPythonHome? #736

Open gilescope opened 4 years ago

gilescope commented 4 years ago

Could there be an os independent wrapper around things like Py_SetPythonHome? It's not the most rustic of ffi interfaces and the sizes change between windows and linux.

davidhewitt commented 4 years ago

Sorry for the delayed response here. What would your example use case be for Py_SetPythonHome ? I've never come across a situation where I've needed to change the python library location (virtualenvs?), but that could just be me.

JJsrc commented 4 years ago

The reason is we are using conda, and therefore python is not in the "standard location". This breaks the python interpreter initialization code, unless you have pythonhome set. And setting it today requires a bit of platform specific code in the vein of : ` unsafe {

[cfg(windows)]

        let raw_ptr = WideCString::from_str(conda_prefix).unwrap().into_raw();
        #[cfg(not(windows))]
        let raw_ptr = WideCString::from_str(conda_prefix).unwrap().into_raw() as *mut i32;
        pyo3::ffi::Py_SetPythonHome(raw_ptr);
    };`

Online python doc explaining: https://docs.python.org/2.0/api/embedding.html

gilescope commented 4 years ago

(We are using setuptools-rust not maturin if thst sheds any light. I am guessing at some point we should switch over?)

Sent with GitHawk

davidhewitt commented 4 years ago

All of the above sounds reasonable. We could easily add some wrappers for these functions.

Just one stupid question - I'm assuming you have reasons why you want to use this function rather than set the PYTHONHOME env var?