Import Python modules into Jython WITHOUT MODIFICATION

Before someone starts ranting:

I have already addressed several similar questions in numerous forums, but they DO NOT answer my question effectively.

Now to the question: Although Java has always been my preferred language, the last few weeks have seen me use Jython for most of my needs. My question has really been forked in two since its inception, and I'm afraid to get a definite answer.

a. Can python modules be imported by AS IS in jython? I read a lot of these questions in different forums, but I never saw a clear answer. I want to know if Python and Jython have some fundamental differences that might not allow this, and whether Python modules can be imported into Jython scripts without any changes.

Q. How to use Jython classes in Java? A simple web search reveals that there used to be a legendary object called jythonc that could compile python code for java bytecode, but it has suffered from extinction since then. The only way to access jython code from Java is to use JSR 223 for scripting using the ScriptEngine class, which raises the following question: is it possible to use the classes that I defined in Jython from Java?

  • If so, how do I do this? For example, how do I extend a class (which was written in Jython) in Java?
  • If this is not the case, is there some kind of project (working or built-in) that allows you to use this function?
+4
source share
1 answer

I will try to answer the question.

Ad. A. It depends. If the module is Python code, it can be used as a regular module. I used this method for some modules, for example python-gnupg . If the module uses C libraries, such as libraries to access the PostgreSQL database, they cannot be used by Jython (they cannot even be installed from the source in environments using only Python without the C compiler and PostgreSQL libs and headers). But for PostgreSQL, I can use the JDBC driver, so for me this is not a problem.

So you have to check if the modules you want to use are simple Python or not. This is easy when most modules have setup.py . You can download such a module, extract it to some directory and use jython setup.py install in the same way as:

 C:\python_libs\fpconst-0.7.2>jython setup.py install running install running build running build_py running install_lib copying build\lib\fpconst.py -> C:\jython2.5.3\Lib\site-packages byte-compiling C:\jython2.5.3\Lib\site-packages\fpconst.py to fpconst$py.class running install_egg_info Writing C:\jython2.5.3\Lib\site-packages\fpconst-0.7.2-py2.5.egg-info C:\python_libs\fpconst-0.7.2>jython Jython 2.5.3 (2.5:c56500f08d34+, Aug 13 2012, 14:48:36) [Java HotSpot(TM) Client VM (Oracle Corporation)] on java1.7.0_09 Type "help", "copyright", "credits" or "license" for more information. >>> import fpconst >>> fpconst <module 'fpconst' from 'fpconst$py.class'> 

For modules that are not supported, some error will be displayed:

 C:\python_libs\pyodbc-2.0.58>jython setup.py install running install running build running build_ext building 'pyodbc' extension error: Compiling extensions is not supported on Jython 
+1
source

All Articles