今天需要在python2.7环境下安装nltk :
但是执行 sudo pip2 install -U nltk 一直报错
Collecting nltk
Downloading https://files.pythonhosted.org/packages/f6/1d/d925cfb4f324ede997f6d47bea4d9babba51b49e87a767c170b77005889d/nltk-3.4.5.zip (1.5MB)
|████████████████████████████████| 1.5MB 17kB/s
ERROR: Command errored out with exit status 1:
command: /usr/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/tmp/pip-install-AapSlY/nltk/setup.py'"'"'; __file__='"'"'/private/tmp/pip-install-AapSlY/nltk/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base pip-egg-info
cwd: /private/tmp/pip-install-AapSlY/nltk/
Complete output (1 lines):
error in nltk setup command: 'install_requires' must be a string or list of strings containing valid project/version requirement specifiers; Expected version spec in singledispatch; python_version < "3.4" at ; python_version < "3.4"
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
分析原因: 显示python setup.py egg_info 原因可能是与setuptools有关, 所以需要尝试更新一下setuptools. 因为pip被用做 python基础组件setuptools 提供的工具easy_install 的替代品, 但是它仍然有大量的功能建立在 setuptools 组件之上。由下图知setuptools之前版本是18.5 升级后是41.2 说明之前版本太老, 所以才会报错, 需要更新setuptools.
sudo pip2 install --user --upgrade setuptools
sudo pip3 install packagename
代表进行全局安装,安装后全局可用。如果是信任的安装包可用使用该命令进行安装。
pip3 install --user packagename
代表仅该用户的安装,安装后仅该用户可用。处于安全考虑,尽量使用该命令进行安装。
解决方法:
(1)需要先更新pip以及setuptools
sudo pip2 install --upgrade pip
sudo pip2 install --upgrade setuptools
显示存在环境错误:
pip install --user --upgrade setuptools
install后加–user显示更新成功
sudo pip install nltk
显示安装成功