问题
MacOX环境下python2.7,通过
sudo pip install --user pandas
命令安装pandas库,反馈
Requirement already satisfied: pandas in ./Library/Python/2.7/lib/python/site-packages (0.24.2)
Requirement already satisfied: pytz>=2011k in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from pandas) (2013.7)
Requirement already satisfied: numpy>=1.12.0 in /Library/Python/2.7/site-packages (from pandas) (1.16.5)
Requirement already satisfied: python-dateutil>=2.5.0 in ./Library/Python/2.7/lib/python/site-packages (from pandas) (2.8.1)
Requirement already satisfied: six>=1.5 in /Library/Python/2.7/site-packages (from python-dateutil>=2.5.0->pandas) (1.13.0)
进入python环境引入库
import pandas
反馈引入失败
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
解决方法
引入失败原因是安装时显示的pandas已存在路径与当前版本python环境查找包路径不一致。可进入当前python环境,通过以下代码,找到当前版本python环境查找包路径
import sys
print(sys.path)
反馈
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages','/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages/pip-19.3.1-py2.7.egg']
以/Library/开头的即为路径地址
'/Library/Python/2.7/site-packages'
之后通过以下命令重新配置pandas环境即可
sudo pip install --target=路径地址 pandas
例如:
sudo pip install --target=/Library/Python/2.7/site-packages pandas
配置结果
配置后再次引入,成功显示版本号