使用Python 3.5.2_2调用NLTK时出现问题?

时间:2016-10-09 17:11:02

标签: python python-3.x nltk python-3.5

当我尝试在python 3.5.2_2中导入nltk时,我收到以下消息:

>>> import nltk
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/nltk/__init__.py", line 117, in <module>
    from nltk.align import *
  File "/usr/local/lib/python3.5/site-packages/nltk/align/__init__.py", line 15, in <module>
    from nltk.align.ibm1 import IBMModel1
  File "/usr/local/lib/python3.5/site-packages/nltk/align/ibm1.py", line 18, in <module>
    from nltk.corpus import comtrans
  File "/usr/local/lib/python3.5/site-packages/nltk/corpus/__init__.py", line 64, in <module>
    from nltk.tokenize import RegexpTokenizer
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/__init__.py", line 65, in <module>
    from nltk.tokenize.regexp   import (RegexpTokenizer, WhitespaceTokenizer,
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/regexp.py", line 201, in <module>
    blankline_tokenize = BlanklineTokenizer().tokenize
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/regexp.py", line 172, in __init__
    RegexpTokenizer.__init__(self, r'\s*\n\s*\n\s*', gaps=True)
  File "/usr/local/lib/python3.5/site-packages/nltk/tokenize/regexp.py", line 119, in __init__
    self._regexp = compile_regexp_to_noncapturing(pattern, flags)
  File "/usr/local/lib/python3.5/site-packages/nltk/internals.py", line 54, in compile_regexp_to_noncapturing
    return sre_compile.compile(convert_regexp_to_noncapturing_parsed(sre_parse.parse(pattern)), flags=flags)
  File "/usr/local/lib/python3.5/site-packages/nltk/internals.py", line 50, in convert_regexp_to_noncapturing_parsed
    parsed_pattern.pattern.groups = 1
AttributeError: can't set attribute

我尝试用pip3重新安装它,我仍然遇到同样的问题......如何在OSX中正确安装NLTK?以及如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

这可能有效

  1. 转到http://www.lfd.uci.edu/~gohlke/pythonlibs/
  2. 下载nltk-3.2.1-py2.py3-none-any.whl
  3. 将命令行中的文件夹更改为下载该whl文件的位置
  4. 现在写C:/windows/python35/scripts/pip install nltk-3.2.1-py2.py3-none-any.whl
  5. 当然,如果他们不匹配

    ,就改变这些路径

答案 1 :(得分:0)

我通过使用pip3 nltk和sklearn升级解决了这个问题:

user@MacBook-Pro-de-User:~$ pip3 install nltk --upgrade
Collecting nltk
  Downloading nltk-3.2.1.tar.gz (1.1MB)
    100% |████████████████████████████████| 1.1MB 675kB/s
Building wheels for collected packages: nltk
  Running setup.py bdist_wheel for nltk ... done
  Stored in directory: /Users/user/Library/Caches/pip/wheels/55/0b/ce/960dcdaec7c9af5b1f81d471a90c8dae88374386efe6e54a50
Successfully built nltk
Installing collected packages: nltk
  Found existing installation: nltk 3.0.0
    Uninstalling nltk-3.0.0:
      Successfully uninstalled nltk-3.0.0
Successfully installed nltk-3.2.1
user@MacBook-Pro-de-User:~$ python3
Python 3.5.2 (default, Sep 28 2016, 18:08:09)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
/usr/local/lib/python3.5/site-packages/sklearn/utils/fixes.py:55: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
  if 'order' in inspect.getargspec(np.copy)[0]:
>>>
user@MacBook-Pro-de-User:~$ clear
user@MacBook-Pro-de-User:~$ pip3 install sklearn --upgrade
Requirement already up-to-date: sklearn in /usr/local/lib/python3.5/site-packages
Collecting scikit-learn (from sklearn)
  Downloading scikit_learn-0.18-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 202kB/s
Installing collected packages: scikit-learn
  Found existing installation: scikit-learn 0.15.2
    Uninstalling scikit-learn-0.15.2:
      Successfully uninstalled scikit-learn-0.15.2
Successfully installed scikit-learn-0.18
user@MacBook-Pro-de-User:~$ clear
user@MacBook-Pro-de-User:~$ python3
Python 3.5.2 (default, Sep 28 2016, 18:08:09)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nltk
>>>