Search code examples
pythonnltkwordnet

Unable to install wordnet with nltk 3.9.0 as importing nltk requires installed wordnet


It is not possible to import nltk, and the solution given by the output required me to import nltk:

>>>import nltk
Traceback (most recent call last):  
File "D:\project\Lib\site-packages\nltk\corpus\util.py", line 84, in __load
    root = nltk.data.find(f"{self.subdir}/{zip_name}")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\project\Lib\site-packages\nltk\data.py", line 579, in find
    raise LookupError(resource_not_found
LookupError:
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')

  For more information see: https://www.nltk.org/data.html

  Attempted to load corpora/wordnet.zip/wordnet/

  Searched in:
    - 'C:\\Users\\me/nltk_data'
    - 'D:\\project\\nltk_data'
    - 'D:\\project\\share\\nltk_data'
    - 'D:\\project\\lib\\nltk_data'
    - 'C:\\Users\\me\\AppData\\Roaming\\nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
**********************************************************************

Basically - I cannot import nltk because wordnet is missing, but in order to download wordnet, I have to import nltk which I cannot, because wordnet is missing.

Noteworthy is, that it throws this exception twice, but with a different traceback -

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\project\Lib\site-packages\nltk\__init__.py", line 156, in <module>
    from nltk.stem import *
  File "D:\project\Lib\site-packages\nltk\stem\__init__.py", line 34, in <module>
    from nltk.stem.wordnet import WordNetLemmatizer
  File "D:\project\Lib\site-packages\nltk\stem\wordnet.py", line 13, in <module>
    class WordNetLemmatizer:
  File "D:project\Lib\site-packages\nltk\stem\wordnet.py", line 48, in WordNetLemmatizer
    morphy = wn.morphy
             ^^^^^^^^^
  File "D:\project\Lib\site-packages\nltk\corpus\util.py", line 120, in __getattr__
    self.__load()
  File "D:\project\Lib\site-packages\nltk\corpus\util.py", line 86, in __load
    raise e
  File "D:\project\Lib\site-packages\nltk\corpus\util.py", line 81, in __load
    root = nltk.data.find(f"{self.subdir}/{self.__name}")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\project\Lib\site-packages\nltk\data.py", line 579, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource wordnet not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('wordnet')

  For more information see: https://www.nltk.org/data.html

  Attempted to load corpora/wordnet

  Searched in:
    - 'C:\\Users\\me/nltk_data'
    - 'D:\\project\\nltk_data'
    - 'D:\\project\\share\\nltk_data'
    - 'D:\\project\\lib\\nltk_data'
    - 'C:\\Users\\me\\AppData\\Roaming\\nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
**********************************************************************

What is the suggested solution in this case?


Solution

  • This bug was introduced in nltk 3.9.0 (released on 18 August 2024) and is a known issue. It was fixed in 3.9.1:

    python3 -m pip install nltk~=3.9.1
    

    The most recent full release prior to 3.9.x was nltk 3.8.1. However, be aware that this version is vulnerable to remote code execution.

    python3 -m pip install nltk==3.8.1