码迷,mamicode.com
首页 > 其他好文 > 详细

Conda虚拟环境嵌套导致的一系列路径问题

时间:2020-09-10 22:42:11      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:pytho   home   程序   ges   for   model   建议   lse   search   

最近在linux上是用conda虚拟环境出现很多问题。

比如使用conda activate py3.7激活环境后发现python搜索路径依然是默认路径:

In [1]: import sys                                                                                                                                                           

In [2]: sys.path                                                                                                                                                             
Out[2]: 
[/home/sherry/anaconda3/bin,
 /home/sherry,
 /home/sherry/git/models/research,
 /home/sherry/git/models/research/slim,
 /home/sherry/anaconda3/lib/python37.zip,
 /home/sherry/anaconda3/lib/python3.7,
 /home/sherry/anaconda3/lib/python3.7/lib-dynload,
 ‘‘,
 /home/sherry/.local/lib/python3.7/site-packages,
 /home/sherry/anaconda3/lib/python3.7/site-packages,
 /home/sherry/anaconda3/lib/python3.7/site-packages/IPython/extensions,
 /home/sherry/.ipython]

同时pip的安装路径也不太对,问题同这篇文章,但是后来里面的方法也失效了,不得不在.bashrc.sh里用alias指定 pip path

(py3.7) sherry@lambda-1:~$ which pip
/home/sherry/anaconda3/envs/py3.7/bin/pip
(py3.7) sherry@lambda-1:~$ type pip
pip is /home/sherry/anaconda3/bin/pip
alias pip=/home/sherry/anaconda3/envs/py3.7/bin/pip

 

参考这篇文章之后发现,问题可能在于环境嵌套。

每次登陆进linux系统后默认进入base环境,再用conda activate激活虚拟环境,会导致发生环境嵌套,具体可以通过conda info中的shell level参数查看:

(py3.7) sherry@lambda-1:~$ conda info

     active environment : py3.7
    active env location : /home/sherry/anaconda3/envs/py3.7
            shell level : 2
       user config file : /home/sherry/.condarc
 populated config files : 
          conda version : 4.8.3
    conda-build version : 3.18.11
         python version : 3.7.7.final.0
       virtual packages : __cuda=11.0
                          __glibc=2.27
       base environment : /home/sherry/anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/sherry/anaconda3/pkgs
                          /home/sherry/.conda/pkgs
       envs directories : /home/sherry/anaconda3/envs
                          /home/sherry/.conda/envs
               platform : linux-64
             user-agent : conda/4.8.3 requests/2.23.0 CPython/3.7.7 Linux/5.4.0-42-generic ubuntu/18.04.3 glibc/2.27
                UID:GID : 1004:1022
             netrc file : None
           offline mode : False

这里shell level是2说明发生嵌套,关于嵌套的更多说明如下:

当已经激活某个环境时,再次 conda activate envname 将发生环境嵌套,而不是切换。

多个环境嵌套时,软件包可能产生异常行为。

Python 需格外小心!若最内层环境没有安装 Python,所有与 Python 有关的程序(尤其是 pip )将逐层向外 fallback 直至 base,很容易造成污染。

可通过 conda info 的 shell level 来检查嵌套情况。

如非特别必要,不建议使用环境嵌套。

默认允许 2 层环境嵌套,可通过 conda config --set max_shlvl number 调至更高或将该特性关闭。

解决方法先用conda deactivate退出base,再激活py3.7,python搜索路径即可恢复正常。

(base) sherry@lambda-1:~$ conda deactivate
sherry@lambda-1:~$ conda activate py3.7
(py3.7) sherry@lambda-1:~$ which pip
/home/sherry/anaconda3/envs/py3.7/bin/pip
(py3.7) sherry@lambda-1:~$ type pip
pip is /home/sherry/anaconda3/envs/py3.7/bin/pip
(py3.7) sherry@lambda-1:~$ conda info

     active environment : py3.7
    active env location : /home/sherry/anaconda3/envs/py3.7
            shell level : 1
       user config file : /home/sherry/.condarc
 populated config files : 
          conda version : 4.8.3
    conda-build version : 3.18.11
         python version : 3.7.7.final.0
       virtual packages : __cuda=11.0
                          __glibc=2.27
       base environment : /home/sherry/anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/sherry/anaconda3/pkgs
                          /home/sherry/.conda/pkgs
       envs directories : /home/sherry/anaconda3/envs
                          /home/sherry/.conda/envs
               platform : linux-64
             user-agent : conda/4.8.3 requests/2.23.0 CPython/3.7.7 Linux/5.4.0-42-generic ubuntu/18.04.3 glibc/2.27
                UID:GID : 1004:1022
             netrc file : None
           offline mode : False
In [1]: import sys                                                                                                                                                           

In [2]: sys.path                                                                                                                                                             
Out[2]: 
[/home/sherry/anaconda3/envs/py3.7/bin,
 /home/sherry,
 /home/sherry/git/models/research,
 /home/sherry/git/models/research/slim,
 /home/sherry/anaconda3/envs/py3.7/lib/python37.zip,
 /home/sherry/anaconda3/envs/py3.7/lib/python3.7,
 /home/sherry/anaconda3/envs/py3.7/lib/python3.7/lib-dynload,
 ‘‘,
 /home/sherry/.local/lib/python3.7/site-packages,
 /home/sherry/anaconda3/envs/py3.7/lib/python3.7/site-packages,
 /home/sherry/anaconda3/envs/py3.7/lib/python3.7/site-packages/IPython/extensions,
 /home/sherry/.ipython]

 

Conda虚拟环境嵌套导致的一系列路径问题

标签:pytho   home   程序   ges   for   model   建议   lse   search   

原文地址:https://www.cnblogs.com/sherrydatascience/p/13583983.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!