一、问题
我在linux服务器上新建一个conda环境后,在pip安装包的时候,老是出现我已在其他目录下安装的提示(如下所示)。我的pip安装路径应该是/home/Bxl/.conda/envs/hjw01/lib/python3.6/site-packages/,而不是/home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages/。
(hjw01) [Bxl@inspur whl]$ pip install requests-2.21.0-py2.py3-none-any.whl
Processing ./requests-2.21.0-py2.py3-none-any.whl
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages/urllib3-1.24.3-py3.6.egg (from requests==2.21.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages/chardet-3.0.4-py3.6.egg (from requests==2.21.0)
Requirement already satisfied: certifi>=2017.4.17 in /home/Bxl/.conda/envs/hjw/lib/python3.6/site-packages (from requests==2.21.0)
Collecting idna<2.9,>=2.5 (from requests==2.21.0)
二、解决方法
我采取的解决思路就是看pip的安装目录是否已经设定,通过一番查找后,发现是安装路径出了问题。解决方法如下:
在激活conda环境的前提下,然后修改 site.py 中的USER_BASE和USER_SITE变量,site.py路径:/home/Bxl/.conda/envs/hjw01/lib/python3.6/site.py,修改后内容如下:(之后重启conda环境)
ImportError exception, it is silently ignored.
"""
import sys
import os
import builtins
import _sitebuiltins
# Prefixes for site-packages; add additional prefixes like /usr/local here
PREFIXES = [sys.prefix, sys.exec_prefix]
# Enable per user site-packages directory
# set it to False to disable the feature or True to force the feature
ENABLE_USER_SITE = None
# for distutils.commands.install
# These values are initialized by the getuserbase() and getusersitepackages()
# functions, through the main() function when Python starts.
USER_SITE = '/home/Bxl/.conda/envs/hjw01/lib/python3.6/site-packages'
USER_BASE = '/home/Bxl/.conda/envs/hjw01'
def makepath(*paths):
dir = os.path.join(*paths)
try:
dir = os.path.abspath(dir)
except OSError:
pass
return dir, os.path.normcase(dir)
def abs_paths():
"""Set all module __file__ and __cached__ attributes to an absolute path"""
for m in set(sys.modules.values()):
if (getattr(getattr(m, '__loader__', None), '__module__', None) not in
('_frozen_importlib', '_frozen_importlib_external')):
continue # don't mess with a PEP 302-supplied __file__
try:
m.__file__ = os.path.abspath(m.__file__)
except (AttributeError, OSError):
pass
try:
m.__cached__ = os.path.abspath(m.__cached__)
except (AttributeError, OSError):
pass