jython

Python for the Java platform (2.7)

   1 wget https://repo1.maven.org/maven2/org/python/jython-standalone/2.7.1/jython-standalone-2.7.1.jar
   2 sudo apt update
   3 sudo apt upgrade
   4 sudo apt install openjdk-17-jdk 
   5 echo "print(\"Hello jython\")" > hello.py
   6 java -jar jython-standalone-2.7.1.jar hello.py 
   7 
   8 alias jython="java -jar jython-standalone-2.7.1.jar"
   9 jython hello.py
  10 # Hello jython
  11 jython -V
  12 # Jython 2.7.1
  13 

Installation

   1 wget https://repo1.maven.org/maven2/org/python/jython-installer/2.7.3/jython-installer-2.7.3.jar
   2 java -jar jython-installer-2.7.3.jar --console
   3 # Welcome to Jython !
   4 # You are about to install Jython version 2.7.3
   5 # (at any time, answer c to cancel the installation)
   6 # For the installation process, the following languages are available: English, German
   7 # Please select your language [E/g] >>> E
   8 # Do you want to read the license agreement now ? [y/N] >>>
   9 # Do you accept the license agreement ? [Y/n] >>>
  10 # The following installation types are available:
  11 #   1. All (everything, including sources)
  12 #   2. Standard (core, library modules, demos and examples, documentation)
  13 #   3. Minimum (core)
  14 #   9. Standalone (a single, executable .jar)
  15 # Please select the installation type [ 1 /2/3/9] >>> 2
  16 # Do you want to install additional parts ? [y/N] >>>
  17 # Do you want to exclude parts from the installation ? [y/N] >>>
  18 # Please enter the target directory >>> /home/vagrant/jython
  19 # Unable to find directory /home/vagrant/jython, create it ? [Y/n] >>>
  20 # Your java version to start Jython is: Debian / 17.0.6
  21 # Your operating system version is: Linux / 5.10.0-20-amd64
  22 # Summary:
  23 #   - mod: true
  24 #   - demo: true
  25 #   - doc: true
  26 #   - src: false
  27 #   - ensurepip: true
  28 #   - JRE: /usr/lib/jvm/java-17-openjdk-amd64
  29 # Please confirm copying of files to directory /home/vagrant/jython [Y/n] >>>
  30 #  70 %
  31 # Generating start scripts ...
  32 # Installing pip and setuptools
  33 #  90 %
  34 # DEPRECATION: A future version of pip will drop support for Python 2.7.
  35 # Looking in links: /tmp/tmpaSLvLl
  36 # Collecting setuptools
  37 # Collecting pip
  38 # Installing collected packages: setuptools, pip
  39 # Successfully installed pip-19.1 setuptools-41.0.1
  40 #  100 %
  41 # Do you want to show the contents of README ? [y/N] >>> 
  42 # Congratulations! You successfully installed Jython 2.7.3 to directory /home/vagrant/jython.
  43 PATH=$PATH:/home/vagrant/jython/bin/
  44 jython -V
  45 # Jython 2.7.3
  46 pip2.7 install cherrypy 
  47 # DEPRECATION: A future version of pip will drop support for Python 2.7.
  48 # Collecting cherrypy
  49 #  Downloading https://files.pythonhosted.org (...)
  50 jython
  51 # Jython 2.7.3 (tags/v2.7.3:5f29801fe, Sep 10 2022, 18:52:49)
  52 # [OpenJDK 64-Bit Server VM (Debian)] on java17.0.6
  53 # Type "help", "copyright", "credits" or "license" for more information.
  54 # >>> import cherrypy
  55 # >>> cherrypy.__version__
  56 # '17.4.2'
  57 

GUI app

app.py

   1 from javax.swing import JButton, JFrame
   2 
   3 frame = JFrame('Hello, Jython!',
   4             defaultCloseOperation = JFrame.EXIT_ON_CLOSE,
   5             size = (300, 300)
   6         )
   7 
   8 def change_text(event):
   9     print 'Clicked!'
  10 
  11 button = JButton('Click Me!', actionPerformed=change_text)
  12 frame.add(button)
  13 frame.visible = True

   1 java -jar jython-standalone-2.7.1.jar app.py 

GIL (Global Interpreter Lock)

https://jython.readthedocs.io/en/latest/chapter19/#no-global-interpreter-lock

No Global Interpreter Lock

Jython lacks the global interpreter lock (GIL), which is an implementation detail of CPython. For CPython, the GIL means that only one thread at a time can run Python code.

Again, Jython does not have the straightjacket of the GIL. This is because all Python threads are mapped to Java threads and use standard Java garbage collection support (the main reason for the GIL in CPython is because of the reference counting GC system). The important ramification here is that you can use threads for compute-intensive tasks that are written in Python.

Java/jython (last edited 2023-06-02 17:06:01 by 127)