segment-any-text / wtpsplit

Toolkit to segment text into sentences or other semantic units in a robust, efficient and adaptable way.
MIT License
667 stars 39 forks source link

ImportError in Python (NNSplit) #45

Closed albertovilla closed 3 years ago

albertovilla commented 3 years ago

Hi, I was trying the simple example in Python from the documentation and I'm getting an ImportError:

from nnsplit import NNSplit
splitter = NNSplit.load("en")

# returns `Split` objects
splits = splitter.split(["This is a test This is another test."])[0]

# a `Split` can be iterated over to yield smaller splits or stringified with `str(...)`.
for sentence in splits:
   print(sentence)

When executing this example I'm getting the following error:

Traceback (most recent call last):
  File "nnsplit.py", line 1, in <module>
    from nnsplit import NNSplit
  File "G:\OneDrive\projects\s\nnsplit.py", line 1, in <module>
    from nnsplit import NNSplit
ImportError: cannot import name 'NNSplit' from partially initialized module 'nnsplit' (most likely due to a circular import) (G:\OneDrive\projects\s\nnsplit.py)

I have installed the packages in a new conda environment, executing pip list installed I have:

pip list installed
Package         Version
--------------- -------------------
certifi         2020.12.5
nnsplit         0.5.7.post0
numpy           1.20.3
onnxruntime     1.7.0
onnxruntime-gpu 1.7.0
pip             21.1.1
protobuf        3.17.1
setuptools      52.0.0.post20210125
six             1.16.0
tqdm            4.61.0
wheel           0.36.2
wincertstore    0.2
albertovilla commented 3 years ago

I have also tried the approach which appears in nnsplit/bindings/python/tests/test_nnsplit.py and it gives a similar error:

import nnsplit

model = nnsplit.NNSplit.load("en")
splits = model.split(["This is a test This is another test."])[0]

for sentence in splits:
    print(sentence)

The error is:

model = nnsplit.NNSplit.load("en")
AttributeError: partially initialized module 'nnsplit' has no attribute 'NNSplit' (most likely due to a circular import)
bminixhofer commented 3 years ago

Hi, thanks for the issue!

I believe the problem is that the file is named nnsplit.py, can you try renaming it? I just tried using numpy in a file called numpy.py and get the same error, so I don't think this is a bug in nnsplit.

albertovilla commented 3 years ago

Hi Benajamin, I think that was an issue on my side... I put that name on the file temporarily and I didn't change it :) Now, the example works, thanks a lot.