问题简述:
环境为deepin15+Anaconda3-5.1.0,使用Anaconda3的pip安装一些python模块时报错ModuleNotFoundError: No module named 'pip',但重新安装pip后问题依旧,发现/usr/share/Anaconda3/lib/python3.6/site-packages中有pip的目录.
定位过程:
在python命令行中打印sys.path如下:
['', '/usr/share/Anaconda3/lib/python36.zip', '/usr/share/Anaconda3/lib/python3.6', '/usr/share/Anaconda3/lib/python3.6/lib-dynload', '/usr/share/Anaconda3/lib/python3.6/dist-packages']
发现并未包含'/usr/share/Anaconda3/lib/python3.6/site-packages'目录,使用strace python3.6(见strace python3.6部分日志.txt)发现读取了多个/usr/share/Anaconda3/lib/中的py文件,找出几个可疑的分析后最终发现是/usr/share/Anaconda3/lib/python3.6/site.py的问题:
For Debian and derivatives, this sys.path is augmented with directories
for packages distributed within the distribution. Local addons go
into /usr/local/lib/python/dist-packages, Debian addons
install into /usr/lib/python3/dist-packages.
/usr/lib/python/site-packages is not used.
if
os.sep
==
'/'
:
if
'VIRTUAL_ENV'
in
os.environ
or
sys.base_prefix
!=
sys.prefix:
sitepackages.append(os.path.join(prefix,
"lib"
,
"python"
+
sys.version[:
3
],
"site-packages"
))
sitepackages.append(os.path.join(prefix,
"local/lib"
,
"python"
+
sys.version[:
3
],
"dist-packages"
))
sitepackages.append(os.path.join(prefix,
"lib"
,
"python3"
,
"dist-packages"
))
# this one is deprecated for Debian
sitepackages.append(os.path.join(prefix,
"lib"
,
"python"
+
sys.version[:
3
],
"dist-packages"
))
else
:
sitepackages.append(prefix)
sitepackages.append(os.path.join(prefix,
"lib"
,
"site-packages"
))
解决方法:
在/usr/share/Anaconda3/lib/python3.6目录下建一个文件夹dist-packages,并在此文件夹中建一个.pth后缀的文件,如site-packages.pth,内容为../site-packages(指向site-packages目录即可).