You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-dev@lucene.apache.org by Roman Chyla <ro...@gmail.com> on 2011/11/13 22:59:33 UTC

set PYTHONPATH programatically from Java?

Hi,

I am using JCC to run Python inside Java. For unittest, I'd like to
set PYTHONPATH environment variable programmatically. I can change env
vars inside Java (using
http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java)
and System.getenv("PYTHONPATH") shows correct values

However, I am still getting "ImportError: no module named...."

If I set PYTHONPATH before starting unittest, it works fine

Is it possible what I would like to do?

Alternatively, if JCC could execute/eval python string, I could set
sys.argv that way


Thanks for any pointers!


  Roman

Re: set PYTHONPATH programatically from Java?

Posted by Roman Chyla <ro...@gmail.com>.
Hi Andi,

Please find attached the patch that adds the eval to JCC.

It uses PyRun_SimpleString which returns 0 or -1, I have verified it
works, on errors it returns -1, unless one sends "raise
SystemException()" -- then it tears down all VMs

For better reporting I'd have to use
http://docs.python.org/c-api/veryhigh.html#PyRun_String

Could you give me some pointers on how to obtain  *globals *locals
parameters? I can modify the patch to use PyRun_String (it would be
nice to have the exceptions -- i think that exceptions will be
reported automatically when the native method is declared as throwing
them)

something like...?

PyObject *module = PyImport_ImportModule("__builtin__");
PyObject *globalFunction = PyObject_GetAttrString(module, "globals");
PyObject *globals = *PyObject_CallFunction(globalFunction, NULL);


Thanks,

  Roman



On Mon, Nov 14, 2011 at 6:12 PM, Andi Vajda <va...@apache.org> wrote:
>
>  Hi Roman,
>
> On Mon, 14 Nov 2011, Roman Chyla wrote:
>
>> so after reading
>> http://docs.python.org/c-api/init.html#PySys_SetArgvEx and the source
>> code for _PythonVM_init i figured it out
>>
>> I have to do:
>>
>> PythonVM.start("/dvt/workspace/montysolr/src/python/montysolr");
>>
>> and the sys.path then contains the parent folder (above montysolr) and
>> i can then set more things by loading some boostrap module
>>
>> but something like
>> http://docs.python.org/c-api/veryhigh.html#PyRun_SimpleString would be
>> much more flexible. Is it something that could be added? I can prepare
>> a patch (as it seems really trivial my knowledge might be sufficient
>> for this :))
>
> Yes, adding an 'eval' method on PythonVM, like the 'instantiate' method,
> would make sense. A patch is welcome !
>
> Andi..
>
>>
>> roman
>>
>> On Mon, Nov 14, 2011 at 1:12 PM, Roman Chyla <ro...@gmail.com>
>> wrote:
>>>
>>> On Mon, Nov 14, 2011 at 4:25 AM, Andi Vajda <va...@apache.org> wrote:
>>>>
>>>> On Sun, 13 Nov 2011, Roman Chyla wrote:
>>>>
>>>>> I am using JCC to run Python inside Java. For unittest, I'd like to
>>>>> set PYTHONPATH environment variable programmatically. I can change env
>>>>> vars inside Java (using
>>>>>
>>>>>
>>>>> http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java)
>>>>> and System.getenv("PYTHONPATH") shows correct values
>>>>>
>>>>> However, I am still getting "ImportError: no module named...."
>>>>>
>>>>> If I set PYTHONPATH before starting unittest, it works fine
>>>>>
>>>>> Is it possible what I would like to do?
>>>>
>>>> Why mess with the environment instead of setting sys.path directly
>>>> instead ?
>>>
>>> That would be great, but I don't know how. I am doing roughly this:
>>>
>>> PythonVM.start(programName)
>>> vm = PythonVM.get()
>>> vm.instantiate(moduleName, className);
>>>
>>> I tried also:
>>> PythonVM.start(programName, new String[]{"-c", "import
>>> sys;sys.path.insert(0, \'/dvt/workspace/montysolr/src/python\'"});
>>>
>>> it is failing on vm.instantiate.... when Python cannot find the module
>>>
>>>>
>>>>> Alternatively, if JCC could execute/eval python string, I could set
>>>>> sys.argv that way
>>>>
>>>> I'm not sure what you mean here but JCC's Java PythonVM.init() method
>>>> takes
>>>> an array of strings that is fed into sys.argv. See _PythonVM_Init()
>>>> sources
>>>> in jcc.cpp for details.
>>>
>>> sorry, i meant sys.path, not sys.argv
>>>
>>> roman
>>>
>>>>
>>>> Andi..
>>>>
>>>
>>
>

Re: set PYTHONPATH programatically from Java?

Posted by Andi Vajda <va...@apache.org>.
  Hi Roman,

On Mon, 14 Nov 2011, Roman Chyla wrote:

> so after reading
> http://docs.python.org/c-api/init.html#PySys_SetArgvEx and the source
> code for _PythonVM_init i figured it out
>
> I have to do:
>
> PythonVM.start("/dvt/workspace/montysolr/src/python/montysolr");
>
> and the sys.path then contains the parent folder (above montysolr) and
> i can then set more things by loading some boostrap module
>
> but something like
> http://docs.python.org/c-api/veryhigh.html#PyRun_SimpleString would be
> much more flexible. Is it something that could be added? I can prepare
> a patch (as it seems really trivial my knowledge might be sufficient
> for this :))

Yes, adding an 'eval' method on PythonVM, like the 'instantiate' method, 
would make sense. A patch is welcome !

Andi..

>
> roman
>
> On Mon, Nov 14, 2011 at 1:12 PM, Roman Chyla <ro...@gmail.com> wrote:
>> On Mon, Nov 14, 2011 at 4:25 AM, Andi Vajda <va...@apache.org> wrote:
>>>
>>> On Sun, 13 Nov 2011, Roman Chyla wrote:
>>>
>>>> I am using JCC to run Python inside Java. For unittest, I'd like to
>>>> set PYTHONPATH environment variable programmatically. I can change env
>>>> vars inside Java (using
>>>>
>>>> http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java)
>>>> and System.getenv("PYTHONPATH") shows correct values
>>>>
>>>> However, I am still getting "ImportError: no module named...."
>>>>
>>>> If I set PYTHONPATH before starting unittest, it works fine
>>>>
>>>> Is it possible what I would like to do?
>>>
>>> Why mess with the environment instead of setting sys.path directly instead ?
>>
>> That would be great, but I don't know how. I am doing roughly this:
>>
>> PythonVM.start(programName)
>> vm = PythonVM.get()
>> vm.instantiate(moduleName, className);
>>
>> I tried also:
>> PythonVM.start(programName, new String[]{"-c", "import
>> sys;sys.path.insert(0, \'/dvt/workspace/montysolr/src/python\'"});
>>
>> it is failing on vm.instantiate.... when Python cannot find the module
>>
>>>
>>>> Alternatively, if JCC could execute/eval python string, I could set
>>>> sys.argv that way
>>>
>>> I'm not sure what you mean here but JCC's Java PythonVM.init() method takes
>>> an array of strings that is fed into sys.argv. See _PythonVM_Init() sources
>>> in jcc.cpp for details.
>>
>> sorry, i meant sys.path, not sys.argv
>>
>> roman
>>
>>>
>>> Andi..
>>>
>>
>

Re: set PYTHONPATH programatically from Java?

Posted by Roman Chyla <ro...@gmail.com>.
hi,

so after reading
http://docs.python.org/c-api/init.html#PySys_SetArgvEx and the source
code for _PythonVM_init i figured it out

I have to do:

PythonVM.start("/dvt/workspace/montysolr/src/python/montysolr");

and the sys.path then contains the parent folder (above montysolr) and
i can then set more things by loading some boostrap module

but something like
http://docs.python.org/c-api/veryhigh.html#PyRun_SimpleString would be
much more flexible. Is it something that could be added? I can prepare
a patch (as it seems really trivial my knowledge might be sufficient
for this :))

roman

On Mon, Nov 14, 2011 at 1:12 PM, Roman Chyla <ro...@gmail.com> wrote:
> On Mon, Nov 14, 2011 at 4:25 AM, Andi Vajda <va...@apache.org> wrote:
>>
>> On Sun, 13 Nov 2011, Roman Chyla wrote:
>>
>>> I am using JCC to run Python inside Java. For unittest, I'd like to
>>> set PYTHONPATH environment variable programmatically. I can change env
>>> vars inside Java (using
>>>
>>> http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java)
>>> and System.getenv("PYTHONPATH") shows correct values
>>>
>>> However, I am still getting "ImportError: no module named...."
>>>
>>> If I set PYTHONPATH before starting unittest, it works fine
>>>
>>> Is it possible what I would like to do?
>>
>> Why mess with the environment instead of setting sys.path directly instead ?
>
> That would be great, but I don't know how. I am doing roughly this:
>
> PythonVM.start(programName)
> vm = PythonVM.get()
> vm.instantiate(moduleName, className);
>
> I tried also:
> PythonVM.start(programName, new String[]{"-c", "import
> sys;sys.path.insert(0, \'/dvt/workspace/montysolr/src/python\'"});
>
> it is failing on vm.instantiate.... when Python cannot find the module
>
>>
>>> Alternatively, if JCC could execute/eval python string, I could set
>>> sys.argv that way
>>
>> I'm not sure what you mean here but JCC's Java PythonVM.init() method takes
>> an array of strings that is fed into sys.argv. See _PythonVM_Init() sources
>> in jcc.cpp for details.
>
> sorry, i meant sys.path, not sys.argv
>
> roman
>
>>
>> Andi..
>>
>

Re: set PYTHONPATH programatically from Java?

Posted by Roman Chyla <ro...@gmail.com>.
On Mon, Nov 14, 2011 at 4:25 AM, Andi Vajda <va...@apache.org> wrote:
>
> On Sun, 13 Nov 2011, Roman Chyla wrote:
>
>> I am using JCC to run Python inside Java. For unittest, I'd like to
>> set PYTHONPATH environment variable programmatically. I can change env
>> vars inside Java (using
>>
>> http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java)
>> and System.getenv("PYTHONPATH") shows correct values
>>
>> However, I am still getting "ImportError: no module named...."
>>
>> If I set PYTHONPATH before starting unittest, it works fine
>>
>> Is it possible what I would like to do?
>
> Why mess with the environment instead of setting sys.path directly instead ?

That would be great, but I don't know how. I am doing roughly this:

PythonVM.start(programName)
vm = PythonVM.get()
vm.instantiate(moduleName, className);

I tried also:
PythonVM.start(programName, new String[]{"-c", "import
sys;sys.path.insert(0, \'/dvt/workspace/montysolr/src/python\'"});

it is failing on vm.instantiate.... when Python cannot find the module

>
>> Alternatively, if JCC could execute/eval python string, I could set
>> sys.argv that way
>
> I'm not sure what you mean here but JCC's Java PythonVM.init() method takes
> an array of strings that is fed into sys.argv. See _PythonVM_Init() sources
> in jcc.cpp for details.

sorry, i meant sys.path, not sys.argv

roman

>
> Andi..
>

Re: set PYTHONPATH programatically from Java?

Posted by Andi Vajda <va...@apache.org>.
On Sun, 13 Nov 2011, Roman Chyla wrote:

> I am using JCC to run Python inside Java. For unittest, I'd like to
> set PYTHONPATH environment variable programmatically. I can change env
> vars inside Java (using
> http://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java)
> and System.getenv("PYTHONPATH") shows correct values
>
> However, I am still getting "ImportError: no module named...."
>
> If I set PYTHONPATH before starting unittest, it works fine
>
> Is it possible what I would like to do?

Why mess with the environment instead of setting sys.path directly instead ?

> Alternatively, if JCC could execute/eval python string, I could set
> sys.argv that way

I'm not sure what you mean here but JCC's Java PythonVM.init() method takes 
an array of strings that is fed into sys.argv. See _PythonVM_Init() sources 
in jcc.cpp for details.

Andi..