1

I can import ntlk and the pos_tag in the python shell just fine like so.

>>> import nltk
>>> from nltk import pos_tag

But when I need to write a script like

import nltk
from nltk import pos_tag

I get a traceback error

Traceback (most recent call last):
File "mynltkfile.py", line 1, in <module>
from nltk import pos_tag
File "/Users/jacksongeller/Desktop/nltktest/mynltkfile.py", line 1, in <module>
from nltk import pos_tag
ImportError: cannot import name pos_tag

I have already done nltk.download() and have downloaded everything. I have also piped numpy

Also if I just import nltk from a script, I get the same traceback

Traceback (most recent call last):
File "mynltkfile.py", line 1, in <module>
import nltk
File "/Users/jacksongeller/Desktop/nltktest/mynltkfile.py", line 1, in <module>
from nltk import pos_tag
ImportError: cannot import name pos_tag

Am I missing a file? If so where should it go? Thanks in advance

user2758113
  • 1,001
  • 1
  • 13
  • 25

1 Answers1

0

From the interpreter, type

import nltk
import sys
print(nltk)
print(sys.executable)

and then create a script with the same contents and run

python script.py

Please post the output.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • The interpreter >>> print(nltk) >>> print(sys.executable) /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python – user2758113 Oct 07 '13 at 20:27
  • And what about the output when run from a script? – unutbu Oct 07 '13 at 20:27
  • the script `code` Traceback (most recent call last): File "test.py", line 1, in import nltk File "/Users/jacksongeller/Desktop/nltk.py", line 1, in ImportError: cannot import name pos_tag – user2758113 Oct 07 '13 at 20:28
  • I believe it had something to do with the directory, i tried placing the script above in my home directory where nltk is stored, and that worked fine. – user2758113 Oct 07 '13 at 20:31
  • Yes, you do have two versions of `nltk` installed. One in `/Library/Python2.7/site-packages` and another in `/Users/jacksongeller/Desktop`. – unutbu Oct 07 '13 at 20:33
  • Do you need both? If you do, you should use [virtualenv](http://stackoverflow.com/q/5844869/190597) and [virtualenvwrapper](https://bitbucket.org/dhellmann/virtualenvwrapper). If not, you should delete the one you do not need so Python will always find the same `nltk` package. – unutbu Oct 07 '13 at 20:34
  • So I deleted both directories of the nltk data and did the following command sudo python -m nltk.downloader -d /usr/share/nltk_data all. It all works now thanks – user2758113 Oct 07 '13 at 20:40