Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Jython refuse to find my Java package?

Tags:

jython

swing

I know it's something silly, but for some reason Jython refuses to find javax.swing. I'm using Java 1.6.0_11. This is my start-up script:

@echo off

"%JAVA_HOME%\bin\java" -Xmx1024M -classpath ".;c:\Projects\Jython2.5.1\jython.jar" org.python.util.jython 

My output looks like:

Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54)
[Java HotSpot(TM) Client VM (Sun Microsystems Inc.)] on java1.6.0_10
Type "help", "copyright", "credits" or "license" for more information.
>>> import javax.swing
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named swing
>>> import javax
>>> dir(javax)
['__name__']
>>>
like image 341
MikeHoss Avatar asked Nov 05 '09 16:11

MikeHoss


People also ask

How do I import libraries into Jython?

class is imported in the following Jython Script. Methods in this class can be called from the Jython script importex.py. Save and execute the above script from the command line to get following output. C:\jython27\bin>jython importex.py Hello World!

Can Jython use Python libraries?

Jython does *not* support third-party Python libraries that use extensions written in C. This means that popular Python libraries like numpy, scipy and scikit-learn will not work in Jython (and, for this reason, will not work in Processing.py).

What version of Python does Jython use?

The Jython project provides implementations of Python in Java, providing to Python the benefits of running on the JVM and access to classes written in Java. The current release (a Jython 2.7. x) only supports Python 2 (sorry). There is work towards a Python 3 in the project's GitHub repository.

How does Jython work?

Jython is a version of the Python programming language that runs on the Java platform. It allows users to write programs in Python and compile them to Java bytecodes that run directly on a Java Virtual Machine, or JVM. It's similar to otherJVM languages like; Scala, Kotlin, Groovy, or Clojur.


2 Answers

Most likely Jython is not scanning your packages. On startup, Jython tries to go through the jars and class files on its path and scan for Java packages. This is necessary because there is no way to look for Java packages by reflection. Package scanning can be deliberately turned off, or you could lack write privileges where it wants to write the cached information out see http://wiki.python.org/jython/PackageScanning for more. The best way to import Java classes is to do so explicitly class by class, like so:

from javax.swing import JFrame

This method should always work, even if package scanning is off or otherwise unable to work, and is the recommended approach (though it can be a bit tedious). If you do want to import packages (or if you want to do "from javax.swing import *" which also depends on package scanning - but is discouraged) you will need to figure out why your package scanning isn't working.

like image 178
Frank Wierzbicki Avatar answered Oct 05 '22 06:10

Frank Wierzbicki


I had similar issues, and it turns out that since the standalone Jython dist does not support caching, it also does not support the "import *" approach. This is not clearly documented anywhere in the official Jython docs, but I concluded this based on a number of different bug reports:

  • https://groups.google.com/forum/?hl=en&fromgroups=#!topic/robotframework-users/6ipB0DYJkvU
  • http://bugs.jython.org/issue1778514
  • http://bugs.jython.org/issue1422
  • http://bugs.jython.org/issue1692579

Notable from that last link:

So as Oti noted, in standalone you must do full imports to succeed.

To fix your issue, use the non-standalone standard jython.jar generated by installing jython using the 'Standard' option.

If you wanted to package and distribute jython.jar with your application, in case a user does not have Jython installed, then you will also need to copy/pase the complete "Lib" folder from the jython installation directory into whichever location you end up placing jython.jar. This enables access to the python stdlib which is not included in the standard jar file.

UPDATE: After playing around more, I think I have a fix to enable "import *" type imports even when using the standalone jar. All that needs to be done is to enable caching!

You can do this by either adding the following options to the jvm when running jython:

-Dpython.cachedir.skip=false -Dpython.cachedir=DESIRED CACHE PATH

(Note that the second argument is optional, and if left blank, a default value will be used)

If you are having an issue running the InteractiveConsole embedded in an app (which is what my problem was) you can add these properties before initializing the console:

    Properties props = new Properties();
    props.put("python.cachedir.skip", "false");
    props.put("python.cachedir", "DESIRED CACHE PATH"); // again, this option is optional
    InteractiveConsole.initialize(System.getProperties(), props, new String[0]);
like image 31
Mikeyg36 Avatar answered Oct 05 '22 05:10

Mikeyg36