KRR-Oxford / DeepOnto

A package for ontology engineering with deep learning and language models.
https://krr-oxford.github.io/DeepOnto/
Apache License 2.0
193 stars 12 forks source link

installation: cannot import java related packages #20

Closed cmdoret closed 5 months ago

cmdoret commented 5 months ago

Describe the bug After following the installation instructions (via conda) of deeponto+pytorch+jpype, I cannot import the Ontology class. It seems that all org.* packages cannot be imported. Setting and exporting JAVA_HOME manually does not seem to fix the issue.

It fails with the following error (full traceback below):

ModuleNotFoundError: No module named 'org.slf4j'

Any advice would be appreciated.

NOTICE: I am not familiar with the combination of python x java

To Reproduce Steps to reproduce the behavior:

  1. conda create -n deeponto python=3.10
  2. conda activate deeponto
  3. conda install pytorch torchvision torchaudio cpuonly -c pytorch -c conda-forge
  4. conda install -c conda-forge jpype1
  5. in python, type from deeponto.onto import Ontology

Expected behavior The Ontology is imported.

Screenshots Traceback below:

➜ ipython
Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.24.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from deeponto.onto import Ontology
Please enter the maximum memory located to JVM [8g]:

INFO:deeponto:8g maximum memory allocated to JVM.
INFO:deeponto:JVM started successfully.
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from deeponto.onto import Ontology

File ~/.local/share/micromamba/envs/deeponto/lib/python3.10/site-packages/deeponto/onto/__init__.py:14
      1 # Copyright 2021 Yuan He. All rights reserved.
      2
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
---> 14 from .ontology import Ontology, OntologyReasoner
     15 from .pruning import OntologyPruner
     16 from .verbalisation import OntologyVerbaliser, OntologySyntaxParser

File ~/.local/share/micromamba/envs/deeponto/lib/python3.10/site-packages/deeponto/onto/ontology.py:47
     45 from java.io import File  # type: ignore
     46 from java.lang import Runtime, System  # type: ignore
---> 47 from org.slf4j.impl import SimpleLogger  # type: ignore
     48 System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "warn")  # set slf4j default logging level to warning
     49 from org.semanticweb.owlapi.apibinding import OWLManager  # type: ignore

ModuleNotFoundError: No module named 'org.slf4j'

Desktop (please complete the following information):

Lawhy commented 5 months ago

Hi @cmdoret, it is possible to solve this issue using Python 3.8 or 3.9 instead, because there might be an issue with jpype in Python 3.10 and later. Also, jpype should be shipped when installing deeponto through pip. I am not sure if the conda version of jpype can work here.

cmdoret commented 5 months ago

Thanks @Lawhy, I tried recreating the conda environment from scratch using to python 3.8 (also tried with 3.9), the pip install deeponto command indeed says:

Successfully installed JPype1-1.5.0  [...] deeponto-0.9.1 [...]

Yet, I still run into the same issue :/

Python 3.8.19 | packaged by conda-forge | (default, Mar 20 2024, 12:47:35)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.3 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from deeponto.onto import Ontology
Please enter the maximum memory located to JVM [8g]:

INFO:deeponto:8g maximum memory allocated to JVM.
INFO:deeponto:JVM started successfully.
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from deeponto.onto import Ontology

File ~/.local/share/micromamba/envs/deeponto38/lib/python3.8/site-packages/deeponto/onto/__init__.py:14
      1 # Copyright 2021 Yuan He. All rights reserved.
      2
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
---> 14 from .ontology import Ontology, OntologyReasoner
     15 from .pruning import OntologyPruner
     16 from .verbalisation import OntologyVerbaliser, OntologySyntaxParser

File ~/.local/share/micromamba/envs/deeponto38/lib/python3.8/site-packages/deeponto/onto/ontology.py:47
     45 from java.io import File  # type: ignore
     46 from java.lang import Runtime, System  # type: ignore
---> 47 from org.slf4j.impl import SimpleLogger  # type: ignore
     48 System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "warn")  # set slf4j default logging level to warning
     49 from org.semanticweb.owlapi.apibinding import OWLManager  # type: ignore

ModuleNotFoundError: No module named 'org.slf4j'

Do you have any other suggestion?

deeponto_py38_pip.txt

Lawhy commented 5 months ago

Hi @cmdoret, it can also be the version of your Java that causes the problem. DeepOnto is using OWL API 4 which requires Java 7 or 8 as the backend. You can also comment line 47 temporarily to see if slf4j is the package that causes the problem. FYI, my JDK version is as follows:

openjdk version "21.0.2" 2024-01-16
OpenJDK Runtime Environment (build 21.0.2+13-Ubuntu-122.04.1)
OpenJDK 64-Bit Server VM (build 21.0.2+13-Ubuntu-122.04.1, mixed mode, sharing)
cmdoret commented 5 months ago

Thanks @Lawhy, I tried commenting out that line, but the same error is then raised by the from org.semanticweb.owlapi.apibinding import OWLManager statement below, which led me to think it was a JPype problem.

Unfortunately, I cannot afford to downgrade java (11) on my machine (required by other software), this dependency would make a strong case for dockerizing deeponto and its dependencies. Did you start / plan anything in that direction, and do you think this would be useful?

Lawhy commented 5 months ago

Hi @cmdoret, have you also tried to test jpype alone? It has some default Java imports to play with. Also, please ensure that there is nothing named org in your paths or Python dependencies, which will override the Java's modules. This happened once before when another person asked me for help. The problem with the Java version might not be the problem as every Java dependency in deeponto is in JAR. My development jdk version is quite new as well.

About dockers, it is a good idea but I currently have no time to implement this.

Thrameos commented 5 months ago

Did you try the statement?

print(jpype.java.lang.System.getProperty('java.class.path'))

If the jar is in the path and the import statement is failing then it may be a conflicting python module. This can be bypassed by placing an alias statement such that some other symbol like "jorg" is used to refer to "org".

Example:

import jpype
import jpype.imports
jpype.imports.registerDomain('mypkg','java')  # Someone has foolishly created a "java" directory so we can't import without a conflict
jpype.startJVM()
from mypkg.lang import String  # Imports java.lang.String
cmdoret commented 5 months ago

@Lawhy you were exactly right... I have a folder named ~/org containing my emacs org-mode notes :/ Removing it solved the issue. It's a very unfortunate limitation, and could be useful to have some kind of warning / error message.

Thanks for your help! :)

cmdoret commented 5 months ago

@Thrameos thank you, your example worked and help me figure out that the issue was elsewhere :+1: