gpt4 book ai didi

Python版本/导入困惑

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:53 26 4
gpt4 key购买 nike

我正在尝试让这个 Python 2.7 代码正常工作。

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

导入错误:没有名为 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 安装内容的站 pip 包目录,现在一切顺利!

>>> import sys
>>> sys.path.append('/usr/local/lib/python2.7/site-packages')

最佳答案

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

(编辑:Pip 必须能够在其 PATH 上找到正确的 Python。我没有想到要深入探讨这一 pip 。)

关于Python版本/导入困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39934909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com