【问题标题】:Python version/import confusionPython 版本/导入混淆
【发布时间】:2016-10-08 16:52:02
【问题描述】:

我正在尝试让这个 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

2016 年 10 月 10 日更新:我通过 brew 安装了 2.7 版本的 Python,它给了我 2.7 pip。

$ /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')

【问题讨论】:

  • 可能您将 nltk 安装到您下载的新 brew python 上,而不是系统上?试试which -a python 看看你有哪些蟒蛇。这整件事是关于 python 最糟糕的事情之一。使用virtualenv 在很大程度上消除它。
  • 这修复了它!现在一切都很好。谢谢你的帮助! >>> import sys >>> sys.path.append('/usr/local/lib/python2.7/site-packages')
  • 而且,我会阅读 virtualenv。再次感谢!
  • 很高兴它修复了它,但不要修改 sys.path,而是尝试启动正确的 python!而不是输入python,输入/usr/local/bin/python(我认为这是您使用自制软件获得的路径,目前不在Mac上,因此无法确认)。

标签: python macos python-3.x pip nltk


【解决方案1】:

由于您有两个 python 发行版,您还需要两个版本的pip。通过which -a pip 找出您的pip 可执行文件在哪里,并在必要时为您的Python 2.7 分发安装pip。然后告诉 Python 2.7 附带的pip(可能是/usr/local/bin/pip)安装nltk

(编辑:Pip 必须能够在其 PATH 上找到正确的 Python。我没想到会进入这个。)

【讨论】:

  • 但是,我只有一个 pip:% which -a pip /Library/Frameworks/Python.framework/Versions/3.5/bin/pip 让我阅读 2.7 文档并安装那个 pip。跨度>
  • 对,我应该在我的回答中解决这个问题。
  • @Sol,因为这个答案不足以解决你的问题,@bananafish 应该写一个更中肯的答案,你应该接受那个答案.我不介意:-)
  • @bananafish 和 alexis 我没关系。
猜你喜欢
  • 2022-01-17
  • 2015-03-13
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2013-08-06
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多