--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/89375b7c-0eb4-4013-abcb-c469a4011619%40googlegroups.com.
Thank you for looking up the methods that exist in the C-API to do it. I'm not quite understanding the use case where you can't set the environment variable before launching the application. Can you elaborate? What operating system(s) are you targeting?
The biggest concern would be if the PYTHON_HOME that is set programmatically would conflict with the other environment variables such as LD_LIBRARY_PATH. The python library, for example libpython2.7.so, is already loaded by the time you get into Jep, so I'm not sure what setting PYTHON_HOME would do after that. But other than that concern, I don't see an issue with adding an ability to set PYTHON_HOME to Jep's API since CPython provides the method.
On Fri, Sep 8, 2017 at 4:34 AM, <ma...@robertosanchez.me> wrote:
By the way, It exists a Python method to set the Python home in the Python C APISo maybe we can call it just like other initial params from PyConfig, in pyembed_preinit() or in pyembed_startup() just before the call to Py_Initialize(), I don't know if this approach has some drawback, but It seems quite simple.--RobertoOn Friday, September 8, 2017 at 10:45:56 AM UTC+2, ma...@robertosanchez.me wrote:Hi, I've been reading some posts in github about how to select the PYTHONPATH programmatically, but if I understood correctly, that's no possible, Jep always use the Python environment configured externally by the system (env. variable PYTHON_HOME or similar).My application is a JavaFX application that is distributed with the JRE, currently I'm using jython as python environment and It works more or less correctly, but I would like to use a "real" cpython env. with the latests enhancements and bugfixes, so It seems that Jep could be a good choice, HOWEVER, It seems that with Jep I cannot distribute my app with a python environment because I cannot select it when I run Jep.Honestly I don't understand why the Python home cannot be selected programmatically (for instance in PyConfig.setPythonHome(...)), I suppose that there are technical constraints that I no see, but It would be a great feature in order to allow an app packaging with all the dependencies.If I'm wrong and I can create an app bundle with all environments and use it from my app, please, let me know.--Roberto
--
You received this message because you are subscribed to the Google Groups "Jep Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project...@googlegroups.com.
To post to this group, send email to jep-p...@googlegroups.com.
public static void main(String[] args) throws JepException {
File pythonHome = new File("src/main/deploy/python-env/");
PyConfig conf = new PyConfig();
conf.setPythonHome(pythonHome);
Jep.setInitParams(conf);
try(Jep jep = new Jep()) {
jep.eval("import platform; print('Platform:', platform.platform())");
jep.eval("print('python_version:', platform.python_version())");
}
}
void loadPythonEnv(const char* pythonHome)
{
char* pyLibFilename = get_python_lib_filename(pythonHome);
if (pyLibFilename == NULL) {
fprintf(stderr, "ERROR: Python lib doesn't found on python home: %s\n", pythonHome);
return;
}
char* pyLibPath = malloc((strlen(pythonHome) + 25)*sizeof(char));
char* pyHomeNormalized = malloc((strlen(pythonHome)+1)*sizeof(char));
strcpy(pyHomeNormalized, pythonHome);
if (pyHomeNormalized[strlen(pyHomeNormalized) - 1] == FILE_SEP) {
pyHomeNormalized[strlen(pyHomeNormalized) - 1] = '\0';
}
sprintf(pyLibPath, "%s%clib%c%s", pyHomeNormalized, FILE_SEP, FILE_SEP, pyLibFilename);
void* pyLibrary = dlopen(pyLibPath, RTLD_NOW | RTLD_LOCAL);
free(pyLibPath);
if(pyLibrary == NULL)
{
free(pyHomeNormalized);
fprintf(stderr, "%s\n", dlerror());
return;
}
Py_SetPythonHome(pyHomeNormalized);
printf("HOME final: %s\n\n", Py_GetPythonHome());
}
Hi Roberto,
I looked over your fork and I don't understand the purpose of the dlopen. I'm not an expert in how the library loading works but from what I have seen by the time you dlopen the libpython that is used with jep will have already been loaded. In the static initializer for Jep.java we do System.loadLibrary("jep"), since jep depends on libpython the dynamic linker will load a libpython and link it with libjep. In my experieince if you don't have a libpython on the library path then this call will fail so at this point you have already selected a libpython. Since your code runs after this, it is to late to change which libpython is linked to libjep.
I think if we truly wanted to be able to select which python to load programatically we would have to delay loading of libjep until after the python home is set and then find libjep and libpython relative to PYTHON_HOME. Based off what you have said, I don't think you need all of that, it sounds like you have a mechanism to load libjep and libpython successfully but your PYTHON_HOME is out of sync. I think you have a good case for just adding a simple API for calling through to Py_GetPythonHome and ignoring the loading of the library. Given the variety of ways to manipulate the library path I think it would be impossible to always guarantee that the libpython and the PYTHON_HOME match but the addition you have suggested would at least give developers a way to bring them back together if they don't align.
Ben
To unsubscribe from this group and stop receiving emails from it, send an email to jep-project+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jep-project/3fff2955-84e2-43b6-9eb8-898f2ba5c833%40googlegroups.com.