Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Python modules in Jython WITHOUT MODIFICATION

Before someone starts ranting:

I have already gone through several similar questions on 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 turning to Jython for most of my needs. My question has actually forked into two since its inception, and I am aching to get a definite answer.

A. Can python modules be imported AS IS in jython? I have read many such questions on different forums, but have never seen a clear response. I want to know whether Python and Jython have some fundamental differences that may not allow this, and whether Python modules can be imported in Jython scripts without ANY modifications, whatsoever.

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

  • If it is, then how do I go about it? For example, how do I extend a class (that has been written in Jython) in Java?
  • If it isn't, then is there some project(working or in-development) that enables this kind of functionality?
like image 210
divs1210 Avatar asked Nov 03 '22 09:11

divs1210


1 Answers

I will try to answer A question.

Ad. A. It depends. If module is a Python code then it can be used as normal module. I have used this way some modules, for example python-gnupg. If module uses some C libraries like libraries to access PostgreSQL database then they could not be used by Jython (they are even impossible to install from source on environments with just Python without C compiler and PostgreSQL libs and headers). But for PostgreSQL I can use JDBC driver so it is no problem for me.

So you must check if modules you want to use are plain Python or not. It is easy while most modules have setup.py. You can download such module, extract it in some directory and use jython setup.py install just like:

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
like image 139
Michał Niklas Avatar answered Nov 08 '22 06:11

Michał Niklas