Title: Unable to use the codecs module when embedding Jython
Type: behaviour Severity: major
Components: Library Versions:
Status: closed Resolution: invalid
Dependencies: Superseder:
Assigned To: Nosy List: amak, fwierzbicki, hendrik, hheimbuerger, pjenvey, zyasoft
Priority: Keywords:

Created on 2011-03-22.16:25:17 by hheimbuerger, last changed 2015-01-07.06:45:22 by zyasoft.

msg6449 (view) Author: Henrik Heimbuerger (hheimbuerger) Date: 2011-03-22.16:25:17
Tested on Jython 2.5.2. Could not set the version in the issue tracker, because no version has been defined for 2.5.2 final.

I'm trying to use the function with mixed results.

Here's what I get when trying from the Jython REPL:

Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_23
Type "help", "copyright", "credits" or "license" for more information.
>>> import codecs
>>> import org.python.core.codecs
>>> import _codecs
<function open at 0x2>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'org.python.core.codecs' has no attribute 'open'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'org.python.modules._codecs' has no attribute 'open'

So here I can import codecs and get the open function, although it's missing on org.python.modules.codecs. The _codecs was just a try, I couldn't find that anywhere in the documentation. It just helped me in the past with the _collections module.

When trying that in my Jython module embedded into a Java application, I get for codecs:

Traceback (most recent call last):
  File "<iostream>", line 21, in <module>
ImportError: No module named codecs

Both org.python.core.codecs and _codecs I can import, but they are lacking the open() function just as in the Jython REPL.

Traceback (most recent call last):
  File "<iostream>", line 363, in transform
AttributeError: type object 'org.python.core.codecs' has no attribute 'open'
msg6450 (view) Author: Philip Jenvey (pjenvey) Date: 2011-03-23.20:31:38 is the public API you should be using, not the others

So the root of the problem is you can't import codecs in your embedded environment. How are you embedding Jython in that environment? If you can't import codecs there then there's a problem with your embedding setup
msg6455 (view) Author: Henrik Heimbuerger (hheimbuerger) Date: 2011-03-24.15:36:26
I guess that's also the reason why 'import collections' does not work, then?

Here's the code I use to integrate the Jython class into Java:

    private MetaModel prepareMetaModel() throws IOException {
        PySystemState engineSys = new PySystemState();
        PythonInterpreter interpreter = new PythonInterpreter();
        InputStream resourceAsStream = Plugin.getResource("/src-py/");

        interpreter.exec("x = MetaModelImpl()");
        Object x = interpreter.get("x").__tojava__(MetaModel.class);
        MetaModel metaModel = (MetaModel) x;

        return metaModel;

I guess the problem could be the empty PySystemState? Strangely, other packages like time seem to work just fine as it is.
msg6531 (view) Author: Hendrik Schreiber (hendrik) Date: 2011-05-28.19:23:10
I'm experiencing the same issue using the plain jython-2.5.2.jar from a maven repo and trying to execute a script containing import codecs via JSR 223:

ImportError: No module named codecs
msg9095 (view) Author: Jim Baker (zyasoft) Date: 2014-10-06.03:19:32
This seems to be a general question of embedding Jython. Perhaps this can be worked around with python.executable properly set?
Date User Action Args
2015-01-07 06:45:22zyasoftsetstatus: open -> closed
resolution: invalid
2014-10-06 03:19:33zyasoftsetmessages: + msg9095
2014-05-04 20:13:12zyasoftsetnosy: + zyasoft
2013-02-26 17:51:48fwierzbickisetnosy: + fwierzbicki
2012-03-19 19:39:17amaksetnosy: + amak
2011-05-28 19:23:10hendriksetnosy: + hendrik
messages: + msg6531
2011-03-24 15:36:26hheimbuergersetmessages: + msg6455
2011-03-23 20:31:38pjenveysetnosy: + pjenvey
messages: + msg6450
2011-03-22 16:25:17hheimbuergercreate