创建conda环境后,发现它没有和系统环境隔离. 本文记录该问题和解决方法.
文章目录
-
-
- 1. 问题产生
- 2. 问题原因
- 3. 解决方案
-
1. 问题产生
新建环境,指定python版本为3.6
$ conda create -n venv3.6 python=3.6
激活环境
$ conda activate venv3.6
(venv3.6)$
使用pip list
查看python包列表时出现系统python的包
(venv3.6)$ pip list
Package Version
------------------- -------------------
argon2-cffi 20.1.0
async-generator 1.10
attrs 20.3.0
backcall 0.2.0
bleach 3.2.1
certifi 2020.11.8
cffi 1.14.3
decorator 4.4.2
defusedxml 0.6.0
entrypoints 0.3
importlib-metadata 1.7.0
ipykernel 5.3.4
ipython 7.16.1
ipython-genutils 0.2.0
ipywidgets 7.5.1
jedi 0.17.2
jsonschema 3.2.0
jupyter 1.0.0
jupyter-client 6.1.7
jupyter-console 6.2.0
jupyter-core 4.6.3
jupyterlab-pygments 0.1.2
mistune 0.8.4
nbclient 0.5.1
nbconvert 6.0.7
nbformat 5.0.8
nest-asyncio 1.4.2
notebook 6.1.5
packaging 20.4
pandocfilters 1.4.3
parso 0.7.1
pbr 5.4.5
pickleshare 0.7.5
pip 20.2.4
prometheus-client 0.8.0
prompt-toolkit 3.0.8
pycparser 2.20
Pygments 2.7.2
pyrsistent 0.17.3
python-dateutil 2.8.1
pyzmq 19.0.2
qtconsole 4.7.7
QtPy 1.9.0
Send2Trash 1.5.0
setuptools 50.3.2.post20201201
six 1.11.0
stevedore 3.2.0
terminado 0.9.1
testpath 0.4.4
tornado 6.1
traitlets 4.3.3
virtualenv 16.7.9
virtualenv-clone 0.5.4
virtualenvwrapper 4.8.4
wcwidth 0.2.5
webencodings 0.5.1
wheel 0.36.0
widgetsnbextension 3.5.1
zipp 3.1.0
python和pip都是环境中的路径,没有问题
(venv3.6)$ which python
/home/user/anaconda3/envs/venv3.6/bin/python
(venv3.6)$ which pip
/home/user/anaconda3/envs/venv3.6/bin/pip
查看sys.path
,包含了用户~/.local/lib/python3.6/site-packages
路径
(venv3.6)$ python
>>> import sys
>>> for p in sys.path:
... print(p)
... /home/user/anaconda3/envs/venv3.6/lib/python36.zip
/home/user/anaconda3/envs/venv3.6/lib/python3.6
/home/user/anaconda3/envs/venv3.6/lib/python3.6/lib-dynload
/home/user/.local/lib/python3.6/site-packages
/home/user/anaconda3/envs/venv3.6/lib/python3.6/site-packages
>>>
2. 问题原因
有待进一步确定
3. 解决方案
目前除了issues448,也没有更好的办法了.
使用venv3.6
环境时,设置环境变量PYTHONNOUSERSITE
为True
.
(venv3.6) user@userUbuntu:~$ export PYTHONNOUSERSITE=True
查看sys.path
,清除了用户site-packages
路劲.
(venv3.6) user@userUbuntu:~$ python
>>> import sys
>>> for p in sys.path:
... print(p)
... /home/user/anaconda3/envs/venv3.6/lib/python36.zip
/home/user/anaconda3/envs/venv3.6/lib/python3.6
/home/user/anaconda3/envs/venv3.6/lib/python3.6/lib-dynload
/home/user/anaconda3/envs/venv3.6/lib/python3.6/site-packages
>>>
不使用venv3.6环境时,则删除环境变量PYTHONNOUSERSITE
即可.
$ unset PYTHONNOUSERSITE