我试图让这个Python 2.7代码起作用。
https://github.com/slanglab/phrasemachine
我已经从github下载并解压缩了repo。以下是我尝试运行代码时会发生什么。
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:没有名为nltk的模块
所以,我需要nltk模块。我安装了这个:
果然,Python 2并不了解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
但是,Python 3可以。
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告诉我nltk已经安装,但是已经安装了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
更新10/10/16:我通过brew安装了2.7版本的Python,这给了我2.7点。
$ /usr/local/bin/pip --version
pip 8.1.2 from /usr/local/lib/python2.7/site-packages (python 2.7)
然后我用那个pip安装了nltk:
$ 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
它说它安装了nltk但警告是有关系的。并且,Python 2.7仍然无法导入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
最后更新!我将Python 2.7指向了Homebrew安装东西的站点包目录,我现在很好!
>>> import sys
>>> sys.path.append('/usr/local/lib/python2.7/site-packages')
答案 0 :(得分:1)
由于你有两个python发行版,你还需要两个版本的pip
。找出pip
可执行文件在which -a pip
的位置,并在必要时为您的Python 2.7版本安装pip
。然后告诉使用Python 2.7的pip
(可能是/usr/local/bin/pip
)来安装nltk
。
(编辑:Pip必须能够在其PATH上找到合适的Python。我没有考虑过这个。)