nltk ImportError that doesn't seem to be a path issue

102 views
Skip to first unread message

emily

unread,
Feb 24, 2017, 2:03:04 AM2/24/17
to nltk-users
I'm using Python3 on OSX10.10 and running everything through Terminal.  I installed nltk, numpy, and then few ntlk packages using nltk.download().  I tried out the brown.words() thing (as suggested on http://www.nltk.org/data.html) to ensure everything was imported properly and it worked.

Then I tried running the scripts found here: https://github.com/dereckson/extract-proper-nouns and I got "ImportError: No module named nltk".  I thought it was a path issue, I read a dozen StackExchange posts but they were all focused on making sure all the paths were correct, but I don't think that can be my problem since when I run python3 in Terminal I can import the Brown package access it.  Or can it?

I would really appreciate any help!

Denzil Correa

unread,
Feb 24, 2017, 3:28:42 AM2/24/17
to nltk-...@googlegroups.com
Could you tell us what is your objective and give an example?

-- Sent from a mobile device. Pardon the typos, brevity.
--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nltk-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

emily

unread,
Feb 24, 2017, 1:16:33 PM2/24/17
to nltk-users
I'm trying to run the scripts to pull out proper nouns from a text (which is my understanding of what they do).  I navigate to the directory in Terminal where the scripts are and input "./extract-proper-nouns proofs.txt > nouns.txt" (proofs.txt being the source text file) and I get "ImportError: No module named nltk"

Is that what you mean by an example?

Constantin Orăsan

unread,
Feb 24, 2017, 3:11:07 PM2/24/17
to nltk-users
Hello,

Do you happen to have 2 versions of python installed on your laptop? You say that you are using python3 but are you starting it by typing python or python3 in the terminal? 

I saw the kind of problem you mention on linux machines which still have python2 as the default version (and can be launched using python), but also package python3, which is usually launched using python3. It is easy to forget to call the correct version of python

Rather than calling the script the way you do, run 
python extract-proper-nouns proofs.txt > nouns.txt
(or try python3)

As good practice, I would rename the script from extract-proper-nouns to extract-proper-nouns.py

Good luck

Constantin Orăsan

unread,
Feb 24, 2017, 3:15:26 PM2/24/17
to nltk-users
Just a word of warning: I had a look at the scripts on github. They are written for python2, not python3. 

In order to get them to run, make sure you change the code so print is a function rather than statement. However, this is completely unrelated to your problem. 

As a test which version of python you use, run the command without parameters. If it successfully displays the help message, then you are running python2. 

Denzil Correa

unread,
Feb 24, 2017, 4:02:00 PM2/24/17
to nltk-...@googlegroups.com

-- Sent from a mobile device. Pardon the typos, brevity.

emily

unread,
Feb 25, 2017, 12:22:50 PM2/25/17
to nltk-users
I am running Python 3 (by typing python3) but yes I do have both installed on my machine.  When I run the command without the arguments (i.e. run only "./extract-proper-nouns", I still get the same "ImportError: No module named nltk".

I didn't realise the scripts were written for python2! ><  Thank you for looking and letting me know!  So perhaps because the scripts are Python 2 they're running that automatically and nltk isn't there because I downloaded nltk to Python 3.  I don't know how to test that though.

emily

unread,
Feb 25, 2017, 12:23:00 PM2/25/17
to nltk-users
The script removes other English words and I think it also can handle multi-word proper names, so I think it does more than just that code it was originally based on.

emily

unread,
Feb 25, 2017, 12:23:02 PM2/25/17
to nltk-users
Progress!  I changed the file to be .py as you suggested, and fixed all the instances of print, and then I ran "python3 extract-proper-nouns.py proofs.txt > nouns.txt" and now I'm getting a new import error: "ImportError: cannot import name 'PunktWordTokenizer'".  The other imports are working and I now can't figure out why this one would be any different...


On Friday, 24 February 2017 15:15:26 UTC-5, Constantin Orăsan wrote:

Denzil Correa

unread,
Feb 25, 2017, 1:16:18 PM2/25/17
to nltk-...@googlegroups.com
Regardless of what it does, you'd know if it is an NLTK issue or not (purpose of this mailing list). It's always a problem merely importing foreign code without understanding the details. Besides, the code is hardly a few lines - do and hence it should be easy. My recommendation would be to write these 10-20 lines of code yourself rather than rely on an external script. You'd spend far lesser time writing the code than making the script work.


-- Sent from a mobile device. Pardon the typos, brevity.

emily

unread,
Feb 25, 2017, 7:55:38 PM2/25/17
to nltk-users
Thanks, that's a good point.  I was able to sort it out that way.  Really appreciate the help, Denzil and Constantin.
Reply all
Reply to author
Forward
0 new messages