使用Conda创建空的虚拟环境,pip list有很多其他的包

最近在使用conda管理环境时出现的问题,弄的环境一团糟

(CenterTrack2) py@pysoft:~/testcase/lijiexin/MOTS2/CenterTrack2$ /home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/bin/pip list
Package               Version  
--------------------- ---------
absl-py               0.11.0   
astor                 0.8.1    
cached-property       1.5.2    
certifi               2016.9.26
cycler                0.10.0   
distlib               0.3.6    
distro                1.5.0    
filelock              3.4.1    
gast                  0.4.0    
google-pasta          0.2.0    
grpcio                1.33.2   
h5py                  3.1.0    
importlib-metadata    4.8.3    
importlib-resources   5.4.0    
Keras                 2.2.4    
Keras-Applications    1.0.8    
Keras-Preprocessing   1.1.2    
kiwisolver            1.3.1    
Markdown              3.3.3    
matplotlib            3.3.3    
numpy                 1.19.4   
opencv-contrib-python 4.2.0.34 
opencv-python         4.4.0.46 
packaging             20.4     
Pillow                8.0.1    
pip                   20.0.2   
platformdirs          2.4.0    
protobuf              3.14.0   
pyparsing             2.4.7    
python-dateutil       2.8.1    
PyYAML                5.3.1    
scikit-build          0.11.1   
scipy                 1.5.4    
setuptools            50.3.2   
six                   1.15.0   
tensorboard           1.14.0   
tensorflow-estimator  1.14.0   
tensorflow-gpu        1.14.0   
termcolor             1.1.0    
typing-extensions     4.1.1    
virtualenv            20.17.1  
Werkzeug              1.0.1    
wheel                 0.35.1   
wrapt                 1.12.1   
zipp                  3.6.0 

在空环境中,一堆没有装过的包,也不存在于环境目录的site-packages下

原因:

可能与pip的默认安装路径有关,使得pip list无法索引到虚拟环境中。

解决方案:

我的方案是直接更改虚拟环境下pip的默认安装路径

1、查看默认安装路径

python -m site
(CenterTrack2) py@pysoft:~/testcase/lijiexin/MOTS2/CenterTrack2$ python -m site
sys.path = [
    '/home/py/testcase/lijiexin/MOTS2/CenterTrack2',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python36.zip',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6/lib-dynload',
    '/home/py/.local/lib/python3.6/site-packages',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6/site-packages',
]
USER_BASE: '/home/py/.local' (exists)
USER_SITE: '/home/py/.local/lib/python3.6/site-packages' (exists)
ENABLE_USER_SITE: True

  默认安装路径指向了’/home/py/.local/lib/python3.6/site-packages’并不是我的虚拟环境CenterTrack2

2、重新设定USER_BASE和USER_SITE

修改 site.py 中的USER_BASE和USER_SITE变量,site.py路径:~/anaconda3/envs/CenterTrack2/lib/python3.6/site.py,修改后内容如下:

原始内容:

import sys
import os
import builtins
import _sitebuiltins
import io

# 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 = None
USER_BASE = None

修改为:

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/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6/site-packages'
USER_BASE = '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2'

修改后再次运行 python -m site 查看,输出内容如下:

(CenterTrack2) py@pysoft:~/testcase/lijiexin/MOTS2/CenterTrack2$ python -m site
sys.path = [
    '/home/py/testcase/lijiexin/MOTS2/CenterTrack2',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python36.zip',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6/lib-dynload',
    '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6/site-packages',
]
USER_BASE: '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2' (exists)
USER_SITE: '/home/py/testcase/lijiexin/DL/anaconda3/envs/CenterTrack2/lib/python3.6/site-packages' (exists)
ENABLE_USER_SITE: True

再次pip list:

(CenterTrack2) py@pysoft:~/testcase/lijiexin/MOTS2/CenterTrack2$ pip list
Package    Version            
---------- -------------------
certifi    2016.9.26          
pip        20.0.2             
setuptools 49.6.0.post20210108
six        1.16.0             
wheel      0.34.2 

干净了。

如果没有效果试一试conda deactivate退出环境再重新进一下。

你可能感兴趣的:(conda,pip,python)