Re: Custom JAR using standalone Jython 2.7.2 doesn't obey JYTHONPATH

Jeff Allen <[email protected]> Sun, 29 Mar 2020 20:43:15 +0100
Newsgroups gmane.comp.lang.jython.devel
Message-ID <[email protected]>
I find initialisation difficult to follow too, although I have 
single-stepped it a hundred times. It may help to see where the 
environment is picked up now in the main program:

https://github.com/jythontools/jython/blob/f77cc106afc4b0b61426ae4212c1e38d20ec942d/src/org/python/util/jython.java#L498

and this simply involves getting things from the variables and setting 
them in the Java properties that get passed into initialisation:

https://github.com/jythontools/jython/blob/f77cc106afc4b0b61426ae4212c1e38d20ec942d/src/org/python/util/jython.java#L709

These are just the system properties in this case, although the 
initialisation is designed to allow them to come from elsewhere. (This 
is where I get lost, but the simple case is simple enough.)

The manifest in the standalone JAR makes org.python.util.jython the main 
class, so it runs when you run the JAR with java -jar, therefore it 
picks up JYTHONPATH in the places I've linked, to be the default value 
of python.path. Or you could (I think) have given -Dpython.path=... to 
java instead.

Sorry this change was a surprise. Of course, there are no major releases 
to come (no 2.8, I mean).

Jeff

Jeff Allen

On 29/03/2020 10:06, Pekka Klärck wrote:
> Hi,
>
> I understand the idea to isolate the JAR from the environment, but I'm
> still a bit surprised that this was changed in a minor release and
> that when I use `java -jar jython-standalone-2.7.2.jar` JYTHONPATH is
> actually obeyed. Anyway, this isn't a big problem for us as we can
> explicitly handle JYTHONPATH ourselves. My current solution is doing
> that on Python side and calling the function that does it as part of
> creating the runner on Java side. You can see it here if you are
> interested:
> https://github.com/robotframework/robotframework/commit/cce18967c8b27a14239112b74b66e57e4584f3b0
>
> I also looked at configuring `python.path` via `PythonInterpreter`. If
> I understood it correctly, I should call
> `PythonInterpreter.initialize` before creating a `PythonInterpreter`
> instance and the `initialize` method needs also arguments in addition
> to properties. Due to how our code is structured and arguments are
> passed nowadays, that looked rather complicated. If there's some
> easier way to set `pytohn.path` perhaps it could work. That said, I'm
> fine with the above solution that seems to work fine in our tests.
>
> Thanks again for Jython 2.7.2 everyone involved!
>
> Cheers,
>      .peke
>
> la 28. maalisk. 2020 klo 13.48 Jeff Allen ([email protected]) kirjoitti:
>> I think this is because you are no longer running the interpreter via the jython.main() programme. Try setting in the Java System property python.path, what you might have put in JYTHONPATH.
>>
>> If you are initialising Jython with your own properties object(s) it should go in the earliest consulted, I suspect.
>>
>> Rationale
>>
>> The design intent in Jython is that it use the registry (Java properties) as the means of configuring the interpreter. (Well, it seems to have been that from early on: I'm inferring a principle from the implementation.) Java promotes a platform-independent world where the Java interpreter can be launched any number of ways, and there isn't necessarily a shell environment in the way Unix and C have led us to expect. System properties, however, are a Java feature so it must always be possible to set them.
>>
>> The main program, that you might invoke from the console (even if it is via the java command rather than the launcher), sits as a bridge between console world and Java world, and so I think it is only there that environment variables should be consulted, rather than in a scattered way. (This was mostly true already, but maybe not for JYTHONPATH.) Implementation of the -E option (ignore environment variables), and of trying to be more faithful to CPython in our main program, is behind making this absolute.
>>
>> Jeff
>>
>> On 27/03/2020 21:59, Pekka Klärck wrote:
>>
>> Hi,
>>
>> In the Robot Framework project
>> (https://github.com/robotframework/robotframework) one of our
>> distributions is a standalone JAR that is built on top of standalone
>> Jython. We've earlier used Jython 2.7.0, but there have been lot of
>> fixes since that and we are now planning to switch to Jython 2.7.2. I
>> have already changed our build scripts and have been running all our
>> acceptance tests using the new JAR.
>>
>> Things have worked pretty well with the new JAR otherwise, but it
>> seems it doesn't obey JYTHONPATH set externally before execution.
>> Executing Python code can see the environment variable, but Python
>> modules in directories listed in it cannot be imported. I just tested
>> that if I build a JAR from same sources with Jython 2.7.0, JYTHONPATH
>> works as expected.
>>
>> The strange thing is that when I tested with the standalone Jython
>> 2.7.2 alone, it did handle JYTHONPATH just fine. This got me thinking
>> that perhaps we are somehow embedding Jython wrong. Are there some
>> know changes related to embedding Jython using PythonInterpreter?
>> Should we perhaps configure it somehow to get JYTHONPATH processed
>> correctly? The code currently just initialized PythonInterpreter
>> without any arguments and then calls `interpreter.exec(code)`. You can
>> find the full code here:
>> https://github.com/robotframework/robotframework/blob/master/src/java/org/robotframework/RobotRunner.java
>>
>> One workaround for us is processing JYTHONPATH in our code and I
>> already tested that it works. If that turns out to be the only
>> solution, I just need to find a way to do that only when using the JAR
>> distribution.
>>
>> Cheers,
>>      .peke
>
>

_______________________________________________
Jython-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-dev