-1

I am using PyCharm and Anaconda. I have installed NTLK with sudo pip install -U nltk and even to make sure since I'm on Mac OS and I saw this previous SO post to also try pip3 install nltk.

However, no matter where I try (PyCharm's terminal, Pycharm's Python, or my own terminal), I cannot get import ntlk to work and always get ModuleNotFoundError: No module named 'ntlk'.

The weird thing is that I actually manage to run some code with a simple "Python test.py" that contains: from nltk.tag import StanfordPOSTagger but whenever I try to import ntlk to be able to then nltk.download('punkt') I get the No module named 'ntlk' error.

Would you know where that is coming from?

Edit:

output of

  1. python -V: Python 3.7.0
  2. python3 -V: Python 3.7.0
  3. pip -V: pip 18.0 from /Users/.../lib/python3.7/site-packages/pip (python 3.7)
  4. pip3 -V: pip 18.0 from /Users/.../lib/python3.7/site-packages/pip (python 3.7)

As for python3 -c "import ntlk; print('ntlk available')" it returns:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'ntlk'

Edit 2:

Output of python -c "import os, sys; print(os.linesep.join(sys.path))"

/Users/.../anaconda3/envs/.../lib/python37.zip /Users/.../anaconda3/envs/.../lib/python3.7 /Users/.../anaconda3/envs/.../lib/python3.7/lib-dynload /Users/.../anaconda3/envs/.../lib/python3.7/site-packages

alvas
  • 115,346
  • 109
  • 446
  • 738
LBes
  • 3,366
  • 1
  • 32
  • 66
  • Try running it `pip install -U nltk` from Anaconda prompt – Eric Zhou Aug 26 '18 at 18:43
  • @EricZhou That's I did, it's the first line of my question ;) – LBes Aug 26 '18 at 18:46
  • I'm not completely sure, but this is probably happening because you have 2 installations of python, an Anaocnda one and a non-Anaconda one. When you run pip outside of the Anaconda prompt, it installs to the non-Anaconda one, but when you run a program, it defaults to the Anaconda one, so the module doesn't show up. There was a good stackoverflow answer about this but I can't find it right now. (what I said might be wrong) – Eric Zhou Aug 26 '18 at 19:05
  • @EricZhou I tried that just in case and I got # All requested packages already installed from anaconda. – LBes Aug 26 '18 at 19:16
  • What are the outputs of `python -V`, `python3 -V`, `pip -V` and `pip3 -V`? What does `python3 -c "import ntlk; print('ntlk available')"` return? – hoefling Aug 26 '18 at 19:19
  • @hoefling thanks for the comment. Please see my edit – LBes Aug 26 '18 at 19:30
  • Another question: what is the output of `python -c "import os, sys; print(os.linesep.join(sys.path))" `? – hoefling Aug 26 '18 at 19:41
  • I suspect your user's site packages dir (`/Users/.../lib/python3.7/site-packages`) is not in `sys.path`, so packages installed with `pip` are not visible to Python. However, I don't think `pip` and Anaconda coexist well - why don't you install packages with `conda install`? `conda install nltk` should work just fine. – hoefling Aug 26 '18 at 19:49
  • Oh, and I often misspell the package, writing `ntlk` instead of `nltk`. Double-check you import the right name - maybe it's only a typo. – hoefling Aug 26 '18 at 19:51
  • @hoefling additional edit to show the output. Tried onda install nltk and it still doesn't work – LBes Aug 26 '18 at 19:54
  • Looks like it's indeed the `sys.path` issue: try `PYTHONPATH=/Users/.../lib/python3.7/site-packages python -c "import nltk; print('nltk available')"`. This should be the same dir that is printed on `pip -V` command, but without the trailing `/pip` part. – hoefling Aug 26 '18 at 19:57
  • @hoefling should I add anaconda3... to PYTHONPATH=/Users/.../lib/python3.7/site-packages ? – LBes Aug 26 '18 at 20:11
  • You mention that you installed NLTK into the Anaconda's env but you also mention that you used `sudo`. I am not familiar with Anaconda but it doesn't sound right to me as `sudo` will take you out of your environment so you should be installing into the system python. Try running `pip install nltk` from pycharm's terminal without using sudo. Also, you can check what python/pip is used by using the `which` command. `which python` and `which pip` should be in the same folder. – Roman Kutlak Aug 26 '18 at 20:35
  • @RomanKutlak All of the commands I have typed were in PyCharm's terminal :) – LBes Aug 26 '18 at 22:05

1 Answers1

3

You have a typo in your import code.

It's NLTK (i.e. import nltk) not ntlk =)

alvas
  • 115,346
  • 109
  • 446
  • 738