2016-10-08 9 views
0

私はこのPython 2.7コードを動作させようとしています。Pythonバージョン/混乱をインポート

https://github.com/slanglab/phrasemachine

私はダウンロードしてgithubのからレポを解凍しました。コードを実行しようとするとどうなりますか?

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については通知されます。

10/10/16更新:私はPythonの2.7バージョンをbrew経由でインストールしました。これは私に2.7ピップを与えます。

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

は、その後、私はそのピップと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がインストールするサイトパッケージディレクトリに向けるように指摘しました。

+1

は、おそらくあなたは、あなたがダウンロードした新しい醸造のpythonにNLTKをインストールし、いないシステム1?どのpythonsを持っているかを知るために 'which -a python'を試してみてください。このすべては、Pythonに関する最悪の事の一つです。大部分を排除するには 'virtualenv'を使います。 – bananafish

+0

これは修正済みです!すべては今良いです。ご協力いただきありがとうございます! >>> import sys >>> sys.path.append( '/ usr/local/lib/python2.7/site-packages') – Sol

+0

そして、私はvirtualenvで読み上げます。再度、感謝します! – Sol

答えて

1

2つのPythonディストリビューションがあるので、pipの2つのバージョンも必要です。あなたのpip実行ファイルがwhich -a pipである場所を探し、必要に応じてPython 2.7配布用にpipをインストールしてください。その後、pipにPython 2.7(おそらく/usr/local/bin/pip)と一緒にnltkをインストールするよう指示します。

(編集:。。ピップはそのPATHに適切なPythonのを見つけることができなければなりません、私はこれに行くことを考えていなかった)

+0

しかし、私は唯一のピップを持っています。 – Sol

+0

私の答えでは、その代替案に取り組んでいたはずです。 – alexis

+0

運がありません。私の質問への更新を見てください。 – Sol