NLTK LookupError when using venv, but works when venv is deactivated

Viewed 19

My NLTK data is acting a bit unpredictably. If I don't activate my venv, and run the following code, everything works

Python 3.10.5 (main, Jul  6 2022, 03:07:55) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> from nltk.corpus import wordnet
>>> wordnet.synsets("test")
[Synset('trial.n.02'), Synset('test.n.02'), Synset('examination.n.02'), Synset('test.n.04'), Synset('test.n.05'), Synset('test.n.06'), Synset('test.v.01'), Synset('screen.v.01'), Synset('quiz.v.01'), Synset('test.v.04'), Synset('test.v.05'), Synset('test.v.06'), Synset('test.v.07')]

If I exit the shell and activate my venv with source venv/bin/activate, I get the following error:

Python 3.10.5 (main, Jul  6 2022, 03:07:55) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>> from nltk.corpus import wordnet
>>> wordnet.synsets("test")
Traceback (most recent call last):
  File "/Users/ME/Desktop/test_py/venv/lib/python3.10/site-packages/nltk/corpus/util.py", line 80, in __load
    try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
  File "/Users/ME/Desktop/test_py/venv/lib/python3.10/site-packages/nltk/data.py", line 675, 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')

  Searched in:
    - '/Users/ME/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - '/Users/ME/Desktop/test_py/venv/nltk_data'
    - '/Users/ME/Desktop/test_py/venv/share/nltk_data'
    - '/Users/ME/Desktop/test_py/venv/lib/nltk_data'
**********************************************************************


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/ME/Desktop/test_py/venv/lib/python3.10/site-packages/nltk/corpus/util.py", line 116, in __getattr__
    self.__load()
  File "/Users/ME/Desktop/test_py/venv/lib/python3.10/site-packages/nltk/corpus/util.py", line 81, in __load
    except LookupError: raise e
  File "/Users/ME/Desktop/test_py/venv/lib/python3.10/site-packages/nltk/corpus/util.py", line 78, in __load
    root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))
  File "/Users/ME/Desktop/test_py/venv/lib/python3.10/site-packages/nltk/data.py", line 675, 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')

  Searched in:
    - '/Users/ME/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - '/Users/ME/Desktop/test_py/venv/nltk_data'
    - '/Users/ME/Desktop/test_py/venv/share/nltk_data'
    - '/Users/ME/Desktop/test_py/venv/lib/nltk_data'
**********************************************************************

If I run the command given, I get a successful response:

>>> nltk.download('wordnet')
[nltk_data] Downloading package wordnet to
[nltk_data]     /Users/ME/nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
True

But when I run wordnet.synsets("test"), I again get the LookupError response.

I tried setting the NLTK_DATA env var.

(venv)Desktop/test_py $  ls /usr/local/share/nltk_data
chunkers   grammars   misc       sentiment  taggers
corpora    help       models     stemmers   tokenizers
(venv)Desktop/test_py $  echo $NLTK_DATA
/usr/local/share/nltk_data

And the correct path is even first when I run nltk.data.path

>>> nltk.data.path
['/usr/local/share/nltk_data', '/Users/ME/nltk_data', '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data', '/Users/ME/Desktop/test_py/venv/nltk_data', '/Users/ME/Desktop/test_py/venv/share/nltk_data', '/Users/ME/Desktop/test_py/venv/lib/nltk_data']

But still, I get the LookupError.

Any advice on what to try next?

1 Answers

I can not duplicate errors on Google Collab so I recommend you try it on Google Collab. Or maybe you can try to reinstall everything. But how I said. I can not make your error on my computer.

Related