1

I'm trying to get this Python 2.7 code to work.

https://github.com/slanglab/phrasemachine

I've downloaded and unzipped the repo from github. Here's what happens when I try to run the code.

phrasemachine$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import phrasemachine
>>> text = "Barack Obama supports expanding social security."  
>>> print phrasemachine.get_phrases(text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "phrasemachine.py", line 253, in get_phrases
    tagger = TAGGER_NAMES[tagger]()
  File "phrasemachine.py", line 166, in get_stdeng_nltk_tagger
    tagger = NLTKTagger()
  File "phrasemachine.py", line 133, in __init__
    import nltk

ImportError: No module named nltk

So, I need the nltk module. I have that installed here:

Sure enough, Python 2 doesn't know about nltk.

phrasemachine$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named nltk

But, Python 3 does.

phrasemachine$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>>

Pip tells me that nltk is already installed, but for 3.5.

$ sudo pip install -U nltk
Requirement already up-to-date: nltk in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk-3.2.1-py3.5.egg

Update 10/10/16: I installed the 2.7 version of Python via brew, which give me the 2.7 pip.

$ /usr/local/bin/pip --version
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)

Then I installed nltk with that pip:

$ sudo /usr/local/bin/pip install -U nltk
Password:
The directory '/Users/me/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/me/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting nltk
  Downloading nltk-3.2.1.tar.gz (1.1MB)
    100% |████████████████████████████████| 1.1MB 683kB/s 
Installing collected packages: nltk
  Running setup.py install for nltk ... done
Successfully installed nltk-3.2.1

It says it installed nltk but the warnings are concerning. And, Python 2.7 still fails to import nltk.

$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import phrasemachine
>>> text = "Barack Obama supports expanding social security." 
>>> print phrasemachine.get_phrases(text)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "phrasemachine.py", line 253, in get_phrases
    tagger = TAGGER_NAMES[tagger]()
  File "phrasemachine.py", line 166, in get_stdeng_nltk_tagger
    tagger = NLTKTagger()
  File "phrasemachine.py", line 133, in __init__
    import nltk
ImportError: No module named nltk
>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named nltk

Final update! I pointed Python 2.7 to the site packages directory where Homebrew installs stuff and I'm now good!

>>> import sys
>>> sys.path.append('/usr/local/lib/python2.7/site-packages')
4
  • 1
    Possibly you installed nltk to the new brew python that you downloaded, not the system one? try which -a python to see which pythons you have. This whole thing is one of the worst things about python. Use virtualenv to largely eliminate it.
    – bananafish
    Oct 10, 2016 at 23:15
  • This fixed it! All is good now. Thanks for your help! >>> import sys >>> sys.path.append('/usr/local/lib/python2.7/site-packages')
    – Sol
    Oct 10, 2016 at 23:27
  • And, I will read up on virtualenv. Thanks, again!
    – Sol
    Oct 10, 2016 at 23:32
  • Glad it fixed it, but instead of modifying sys.path, try launching the right python! instead of typing python, type /usr/local/bin/python (I think that's the path you get with homebrew, not on a mac at the moment so can't confirm).
    – bananafish
    Oct 10, 2016 at 23:39

1 Answer 1

1

Since you have two python distributions, you also need two versions of pip. Find out where your pip executables are with which -a pip, and install pip for your Python 2.7 distribution if necessary. Then tell the pip that goes with Python 2.7 (perhaps /usr/local/bin/pip) to install the nltk.

(Edit: Pip must be able to find the proper Python on its PATH. I hadn't thought to go into this.)

4
  • But, I only have one pip: % which -a pip /Library/Frameworks/Python.framework/Versions/3.5/bin/pip Let me read the 2.7 docs and install that pip.
    – Sol
    Oct 8, 2016 at 21:39
  • Right, I should have addressed that alternative in my answer.
    – alexis
    Oct 8, 2016 at 21:57
  • @Sol, since this answer wasn't enough to fix your problem, @bananafish should write a more to-the-point one and you should accept that one instead. I won't mind :-)
    – alexis
    Oct 11, 2016 at 12:32
  • @bananafish and alexis That's fine with me.
    – Sol
    Oct 11, 2016 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.