How to install a PIP package into Jython

I am trying to use the cec PIP package found here. I have the following code in $OPENHAB_CONF/automation/jsr223/python/personal/test_cec_command.py:

#!/usr/bin/python

import cec

cec.init()

tv = cec.Device(cec.CECDEVICE_TV)
tv.power_on()

This gives the error output in the logs, because it cannot find the cec package:

2020-04-01 20:57:29.453 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/test_cec_command.py': ImportError: No module named
 cec in <script> at line number 3

I did install it and running it as openhabe user works as expected:

sudo -u openhab pip install cec
sudo -u openhab ./test_cec_command.py

This link suggested that PIP should work, but I was unable to find more info on how to install it for Jython: Adding python modules to jython

Thanks in advance for any help,
Michael

  • Platform information:
    • Hardware: Raspberry Pi 4
    • OS: OpenHABian 1.5
    • openHAB version: OpenHAB 2.5

That particular module is not pure Python but actually has a compiled component.

Use this to install it…

sudo -u openhab pip2 install --ignore-installed --install-option="--prefix=$OPENHAB_CONF/automation/lib/python/personal" PYTHON_PACKAGE_NAME

… or copy the package manually from wherever it was installed on your system
… or create a symlink to the package
… or add the Python2 packages directory to the python.path

So does does this mean the installation methods that @5iver just suggested, will only result in a non-working package, when I try using it? (using the package I meant, if I get it to install …)

You need the library and compile parts listed on the GitHub page. Perhaps you somehow need to symlink Jython so it appears as Python 2.
Even that that may still not work, especially sonce the project says it has not been verified on a Pi,

The symlinking is in order to compile it for Jython? Because like I said: It already works where run in Python 2. So I have tested it.

If you already have it compiled, I would think you could just copy it & its dependencies.

Have you tried running the script in Jython? Files with a .py extension will be associated with Python (plus you have a #!), so you’d need to either run it from a Jython console or use…

sudo -u openhab jython test_cec_command.py

How is your Jython installed and which version?

BTW, in your scripts used by OH, don’t include…

#!/usr/bin/python

That makes sense. I am completely new to Jython. I don’t even have the jython command in my path:

$ sudo -u openhab jython test_cec_command.py
sudo: jython: command not found

Do I need to configure something for this? Perhaps I should do some long reading before continuing. Perhaps you could point me to an entry point to some relevant resources (Jython related and OpenHAB specific)? That’d be great!

How did you install it? If you used a standalone jar, then there’s more that you could do. However, if you are new to Jython/Python, then I think it would be best to back up and keep things simple. Once the package is in the python.path, you can run it through OH (if it is compatible with Jython). BTW, Jython 2.7.2 had been released and has better compatibility.

Have you read through this?

https://openhab-scripters.github.io/openhab-helper-libraries/index.html

I did not install Jython at all. I thought, that since OpenHAB uses it, it is installed. But now I realize I did not install any package for it. It was just the JAR. I am somewhat familiar with Python BTW - just not Jython.

I will try adding the package to python.path. It is currently located here:

/home/openhabian/.local/lib/python2.7/site-packages

I have only looked at the start of it. I will read through it before continuing. I hope that saves a lot of questions. Thanks.

This may help you with the install of Jython and the helper libraries…

I’ve done some work on this lately to move the bundle to openhab-addons rather than openhab-core and to split out the helper libraries. The new version uses 2.7.2 and is working great.

Give a shout if you have any questions!