[nltk-users] Getting NLTK to import

53 views
Skip to first unread message

Michael Scharf

unread,
Apr 26, 2010, 3:30:59 PM4/26/10
to nltk-...@googlegroups.com
Hi All,

I'm new to the list, and new to Python, but not new to NLP (although a
few years removed from it and from CS generally).

I've gotten IDLE up and have done some hello worlding.

I am now trying to get the NLTK working on an iMac 3.06 GHz Core 2 Duo
running running OS X 10.6.3. The Python version I have up is 2.6.5.
(There may be a slightly older pre-install with faulty IDLE lingering
somewhere on the machine.)

I've installed Macports, and through it I installed NLTK by saying

$ sudo port install nltk

which worked.

But when, in a Python session, I tried to

>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nltk

is what I got.

Macports says if that happens that I should

$ cd /tmp/nltk-installer

which worked. And that I should then

$ sudo python setup.py install

which yielded

Traceback (most recent call last):
File "setup.py", line 13, in <module>
import nltk
File "/private/tmp/nltk-installer/nltk/__init__.py", line 92, in <module>
from yamltags import *
File "/private/tmp/nltk-installer/nltk/yamltags.py", line 1, in <module>
import yaml
ImportError: No module named yaml

So I did this:

$ port search yaml

And got:

libyaml @0.1.3 (devel)
A YAML Parser.

p5-data-phrasebook-loader-yaml @0.09 (perl)
Abstract your queries ... with YAML!

p5-test-yaml-meta @0.15 (perl)
Validation of the META.yml file in a distribution.

p5-test-yaml-valid @0.04 (perl)
Test for valid YAML

p5-yaml @0.68 (perl)
YAML loader/dumper module

p5-yaml-libyaml @0.32 (perl)
Perl YAML Serialization using XS and libyaml

p5-yaml-syck @0.99 (perl)
A fast, lightweight YAML loader and dumper

p5-yaml-tiny @1.41 (perl)
Read/Write YAML files with as little code as possible

py-yaml @3.05 (python, devel)
YAML 1.1 parser and emitter for Python

py25-yaml @3.08 (python, devel)
YAML 1.1 parser and emitter for Python

py26-yaml @3.08 (python, devel)
YAML 1.1 parser and emitter for Python

rb-bee @0.5.1 (ruby, devel)
Bee is a build tool running YAML files

syck @0.55 (textproc, devel)
An extension for reading and writing YAML

yaml-mode.el @0.0.3 (editors)
An emacs major mode for editing yaml files.

Found 14 ports.

So I then did:

$ sudo port install py26-yaml
---> Computing dependencies for py26-yaml
---> Cleaning py26-yaml

closed and re-opened the terminal, and tried it all again.

But I still got the same

Traceback (most recent call last):
File "setup.py", line 13, in <module>
import nltk
File "/private/tmp/nltk-installer/nltk/__init__.py", line 92, in <module>
from yamltags import *
File "/private/tmp/nltk-installer/nltk/yamltags.py", line 1, in <module>
import yaml
ImportError: No module named yaml

Now I've rebooted, and I can't

$ cd /tmp/nltk-installer

anymore, and the only error I get is

>>> import nltk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nltk

I don't know what to try next.

Many thanks,
Mike S.

--
You received this message because you are subscribed to the Google Groups "nltk-users" group.
To post to this group, send email to nltk-...@googlegroups.com.
To unsubscribe from this group, send email to nltk-users+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nltk-users?hl=en.

Richard Careaga

unread,
Apr 26, 2010, 3:58:59 PM4/26/10
to nltk-...@googlegroups.com
Good news is that I've got your hardware/OS setup with nltk working. Bad news is that finding your $PATH can be hard.

First off, if you've 'updated' the distro Python 2.6.1, you may run into more problems than necessary. In my case, the very slick ActiveState version wasn't enough to be worth the install headaches I encountered.

Second, you need to know more than you probably care to about defaults in your file system.

Find out which python is first in your path:

mbt:~ roc$ which python
/usr/bin/python

Next, make sure that its directory comes in your path before any competing versions:

mbt:~ roc$ which python
/usr/bin/python

Modify your login shell (such as .bash_profile) to add a PYTHONPATH

mbt:~ roc$ echo $PYTHONPATH
/Library/Python/2.6/site-packages:/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages

(after you do this, either log out or, more simply, from your home directory:

$ source .bash_profile

)

Now fire up python, noting its version:

mbt:~ roc$ !!
python
\Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

then

>>> import sys
>>> >>> sys.path
['', '/Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg', '/Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/mysql_connector_python-0.1.2_devel-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/numpy-1.4.0.dev7542_20091216-py2.6-macosx-10.6-universal.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib-1.0.svn_r8037-py2.6-macosx-10.6-universal.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/scipy-0.7.1_20091217-py2.6-macosx-10.6-universal.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pymc-2.1alpha_20091216-py2.6-macosx-10.6-universal.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/readline-2.5.1-py2.6-macosx-10.6-universal.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/ipython-0.11.bzr.r1205-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nose-0.11.0.dev_r560-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/DateUtils-0.5.1-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pytz-2010e-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/python_dateutil-1.4.1-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pydot-1.0.2-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pyparsing-1.5.2-py2.6.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PyYAML-3.09-py2.6-macosx-10.3-fat.egg', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/nltk-2.0b8-py2.6.egg', '/Library/Python/2.6/site-packages/setuptools-0.6c11-py2.6.egg', '/Library/Python/2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg', '/Library/Python/2.6/site-packages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages', '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python26.zip', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Users/roc/.local/lib/python2.6/site-packages', '/Users/roc/.local/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/PyObjC', '/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/wx-2.8-mac-unicode']
>>>

This tells you all the places that python will look for modules when it tries to import. If you peer closely, you can see my PyYAML

Since your set-up is not finding PyYAML, you should see it when you sys.path, but you should see where it wants to be. Now all (!) you have to do is to figure out where your version of python is trying to install it and conform your PATH and PYTHONPATH directives accordingly.

But, as a friend in Mac, I say if you have to nuke your existing Python installation and reinstall from the installation X-code disk taking all the defaults. Life will be ever so much easier.



Fire up python and see wher

Michael Scharf

unread,
Apr 26, 2010, 5:03:08 PM4/26/10
to nltk-...@googlegroups.com
Richard, thank you. I'm still in the thick of it, and may have to
call my friend who has real op chops. Will report back.

Thanks again,
Mike
Reply all
Reply to author
Forward
0 new messages