Skip to content

Instantly share code, notes, and snippets.

@sillyfrog
Created November 10, 2018 22:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sillyfrog/bc2e8e2546247a616d97078f1e4fa060 to your computer and use it in GitHub Desktop.
Save sillyfrog/bc2e8e2546247a616d97078f1e4fa060 to your computer and use it in GitHub Desktop.
pip in Jython

To use pip, you must be running Jython v2.7.1 (downloaded from here: http://search.maven.org/remotecontent?filepath=org/python/jython-standalone/2.7.1/jython-standalone-2.7.1.jar). This is not compatible with Esper, so be sure to remove all of these files before upgrading, ie:

rm -rf ./automation/jsr223/000_components/000_Esper.py ./automation/lib/python/esper

With v2.7.1 installed, we can run the Jython from the openHAB installation to install pip

java -jar /openhab/conf/automation/jython/jython-standalone-2.7.1.jar -m ensurepip

Important: Do not upgrade pip, v18.1 in Jython/openHAB appears to have issues with some packages, v9.0.1 continues to work OK.

Ignore errors such as:

  Failed to open /openhab/conf/automation/jython/bin/jython

This is pip/Python trying to run jython as a shell application, which as it's running in Java won't work (there's probably an easy solution using a wrapper script, but as the only goal is to install packages, I have not done any work to make this happen)

You can then install any (pure Python) packages you want, for example, to install Requests:

java -jar /openhab/conf/automation/jython/jython-standalone-2.7.1.jar -m pip install requests

Note: you may have to restart openHAB to get access to the new libraries as the module cache is not reloaded. If nothing has attempted to import the module, it should just work with a reload of the script, however a full restart of the openHAB container is safest to ensure it's pulling all of the new modules in.

@KimSeungSu81
Copy link

KimSeungSu81 commented Mar 3, 2021

hi.

  1. java -jar jython-standalone-2.7.1.jar -m ensurepip --> OK
  2. java -jar jython-standalone-2.7.1.jar -m pip install --upgrade pip --> OK
  3. java -jar jython-standalone-2.7.1.jar -m pip install python-can --> ImportError: No module named _winreg
    An installation error occurs.
    Is there a solution?

Error :
Traceback (most recent call last):
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\jython-standalone-2.7.1.jar\Lib\runpy.py", line 161, in _run_module_as_main
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\jython-standalone-2.7.1.jar\Lib\runpy.py", line 72, in run_code
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_main
.py", line 23, in
from pip._internal.cli.main import main as _main # isort:skip # noqa
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\cli\main.py", line 10, in
from pip._internal.cli.autocompletion import autocomplete
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\cli\autocompletion.py", line 9, in
from pip._internal.cli.main_parser import create_main_parser
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\cli\main_parser.py", line 7, in
from pip._internal.cli import cmdoptions
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\cli\cmdoptions.py", line 25, in
from pip._internal.cli.progress_bars import BAR_TYPES
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\cli\progress_bars.py", line 12, in
from pip._internal.utils.logging import get_indentation
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\utils\logging.py", line 18, in
from pip._internal.utils.misc import ensure_dir
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\utils\misc.py", line 34, in
from pip._internal.locations import get_major_minor_version, site_packages, user_site
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\locations.py", line 30, in
USER_CACHE_DIR = appdirs.user_cache_dir("pip")
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_internal\utils\appdirs.py", line 23, in user_cache_dir
return _appdirs.user_cache_dir(appname, appauthor=False)
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_vendor\appdirs.py", line 300, in user_cache_dir
path = os.path.normpath(_get_win_folder("CSIDL_LOCAL_APPDATA"))
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip_vendor\appdirs.py", line 474, in _get_win_folder_from_registry
import _winreg
ImportError: No module named _winreg

@sillyfrog
Copy link
Author

@KimSeungSu81 - Be sure to not run the --upgrade pip - I had issues with the current pip. I would suggest you remove the Python files and start again.

It's been a loooong time since I've used OpenHab (I'm now using Home Assistant - it's Python based, and with AppDaemon, I can do everything I want), so my memory on this is a bit rusty...

@KimSeungSu81
Copy link

java 8, spring,
maven pom.xml : jython-standalone / 2.7.1
do not install python.
windows cmd : C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1>

If you don't do --upgrade pip.

  1. Delete installed library.
  2. java -jar jython-standalone-2.7.1.jar -m ensurepip-> OK
  3. java -jar jython-standalone-2.7.1.jar -m pip install python-can

--If you do not --upgrade pip, the following error occurs.
Error :
Collecting python-can
Using cached https://files.pythonhosted.org/packages/97/dd/5e5ae96db41ba57dde127e0600c3d324239ed692e167296c5fdb992cbf41/python-can-3.3.4.tar.gz
�[31m Error [Errno 2] No such file or directory while executing command python setup.py egg_info�[0m
�[31mException:
Traceback (most recent call last):
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\commands\install.py", line 324, in run
requirement_set.prepare_files(finder)
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\commands\install.py", line 324, in run
requirement_set.prepare_files(finder)
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\req\req_set.py", line 376, in prepare_files
discovered_reqs.extend(self._prepare_file(
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\req\req_set.py", line 376, in prepare_files
discovered_reqs.extend(self._prepare_file(
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\req\req_set.py", line 634, in prepare_file
abstract_dist.prep_for_dist()
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\req\req_set.py", line 129, in prep_for_dist
self.req_to_install.run_egg_info()
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\req\req_install.py", line 435, in run_egg_info
call_subprocess(
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\Lib\site-packages\pip\utils_init
.py", line 665, in call_subprocess
proc = subprocess.Popen(
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\jython-standalone-2.7.1.jar\Lib\subprocess.py", line 859, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Users\seungsu2.kim.m2\repository\org\python\jython-standalone\2.7.1\jython-standalone-2.7.1.jar\Lib\subprocess.py", line 1369, in _execute_child
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT))
OSError: [Errno 2] No such file or directory�[0m
�[33mYou are using pip version 9.0.1, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.�[0m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment