Anaconda不使用激活环境中的软件包 [英] Anaconda does not use package from activated environment

查看:113
本文介绍了Anaconda不使用激活环境中的软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在bash终端中有一个conda环境,带有一个Intel Python Distribution解释器.但是,在导入软件包时,它们是从看起来像系统默认Python的用户目录而非环境的目录中导入的.查看版本差异和pandas软件包的__spec__来源.

I have a conda environment, in a bash terminal, with an Intel Python Distribution interpreter. However, when importing packages, they are imported from what looks to be the user directory of the system default Python, not the environment. Take a look at the version discrepancy and the __spec__ origin of the pandas package.

 ~  $  conda activate idp
 ~  $  which python
~/anaconda3/envs/idp/bin/python
 ~  $  python
Python 3.6.8 |Intel Corporation| (default, Mar  1 2019, 00:10:45) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
Intel(R) Distribution for Python is brought to you by Intel Corporation.
Please check out: https://software.intel.com/en-us/python-distribution
>>> import pandas
>>> pandas.__version__
'0.22.0'
>>> pandas.__spec__
ModuleSpec(name='pandas', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7f509e184ba8>, origin='/home/torstein/.local/lib/python3.6/site-packages/pandas/__init__.py', submodule_search_locations=['/home/torstein/.local/lib/python3.6/site-packages/pandas'])
>>> 
 ~  $  conda list | head -n 3
# packages in environment at /home/torstein/anaconda3/envs/idp:
#
# Name                    Version                   Build  Channel
 ~  $  conda list | grep pandas
pandas                    0.24.1                   py36_3    intel
 ~  $  conda env list
# conda environments:
#
base                     /home/torstein/anaconda3
idp                   *  /home/torstein/anaconda3/envs/idp
py36                     /home/torstein/anaconda3/envs/py36

在使用base环境时,不会发生这种情况.软件包(例如pandas)是从正确的路径导入的:

When using the base environment, this does NOT happen; packages (e.g. pandas) are imported from the correct path:

 ~  $  conda deactivate
 ~  $  conda env list
# conda environments:
#
base                  *  /home/torstein/anaconda3
idp                      /home/torstein/anaconda3/envs/idp
py36                     /home/torstein/anaconda3/envs/py36

 ~  $  which python
~/anaconda3/bin/python
 ~  $  python
Python 3.7.0 (default, Oct  9 2018, 10:31:47) 
[GCC 7.3.0] :: Anaconda custom (64-bit) on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
pan>>> pandas.__version__
'0.23.4'
>>> pandas.__spec__
ModuleSpec(name='pandas', loader=<_frozen_importlib_external.SourceFileLoader object at 0x7fad808a8e80>, origin='/home/torstein/anaconda3/lib/python3.7/site-packages/pandas/__init__.py', submodule_search_locations=['/home/torstein/anaconda3/lib/python3.7/site-packages/pandas'])

.bashrc的相关部分(路径中未明确设置anaconda)

The relevant part of .bashrc (no anaconda explicitly set in path):

export PATH="/home/torstein/.cargo/bin:$PATH"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/intel/lib/intel64_lin:/opt/intel/compilers_and_libraries_2018.0.128/linux/mkl/lib/intel64_lin/"

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/torstein/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/torstein/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/home/torstein/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/torstein/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup

分别为baseidp envs生成以下$PATH:

Which yields these $PATHs, for the base and idp envs respectively:

 ~  $  echo $PATH
/home/torstein/anaconda3/bin:/home/torstein/anaconda3/condabin:/home/torstein/.cargo/bin:/home/torstein/.cargo/bin:/home/torstein/anaconda3/bin:/home/torstein/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/torstein/.local/bin:/home/torstein/bin
 ~  $  conda activate idp
 ~  $  echo $PATH
/home/torstein/anaconda3/envs/idp/bin:/home/torstein/anaconda3/condabin:/home/torstein/.cargo/bin:/home/torstein/.cargo/bin:/home/torstein/anaconda3/bin:/home/torstein/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/torstein/.local/bin:/home/torstein/bin

我要导入的pandas位于 此处,该位置应为

The pandas that I do want to import is located here, where it should be:

/home/torstein/anaconda3/envs/idp/lib/python3.6/site-packages/pandas

推荐答案

诊断

PATH中似乎有(或曾经有)另一个Python 3.6,我怀疑Conda依赖解析器最终以某种方式将某些软件包解析为该替代site-packages,并且无意中将该目录包含在sys.path中.这似乎是一个已知问题.

Diagnosis

There appears to be (or have been) another Python 3.6 in the PATH, and I suspect that somehow the Conda dependency resolver ended up resolving some packages to this alternative site-packages and inadvertently including this directory in sys.path. This appears to be a known issue.

我认为这是因为pandas模块是从此处加载的:

The reason I believe this is because the pandas module is being loaded from here:

/home/torstein/.local/lib/python3.6/site-packages/pandas

/home/torstein/.local/lib/python3.6/site-packages/pandas

如果您签入Python

If you check in Python

import sys

sys.path

我希望它应该显示上面的目录.

I expect that this should show the above directory.

由于据报道PYTHONPATH是空的(应该是!),所以这不是造成此错误加载的原因,因此,我认为正是Conda以某种方式配置了env.

Since it was reported that PYTHONPATH is empty (as it should be!), that can't be responsible for this misloading, hence I think it was Conda that somehow configured the env this way.

此外,您的Python 3.7 env不受影响的事实很可能是因为您无法跨不同的次要版本加载模块.

Also, the fact that your Python 3.7 env is unaffected is likely because you can't load modules across different minor versions.

您需要以某种方式摆脱这种依赖性.有几件事可以尝试

Somehow you need to get rid of that dependency. There are a few things to try

  1. 从您的PATH中删除该/home/torstein/.local/.但是,这可能会导致其他问题.大概在PATH中有它,因为那里还有其他非开发性的东西.
  2. 专门转储该site-packages目录.在评论中,有人说这是不再使用的全局Python安装的残余,因此摆脱它似乎是一件好事.不过,请备份它,以防其他依赖项.
  3. 在导入模块之前从sys.path中清除此路径.不确定是否有干净的方法可以做到这一点.
  1. Remove that /home/torstein/.local/ from your PATH. This could cause other issues though. Presumably you have it in PATH because you have other non-development things in there.
  2. Dump specifically that site-packages directory. In comments, it was stated that this is residual from a global Python installation no longer in use, so it seems like a good thing to just get rid of. Do back it up, though, in case there are other dependencies on it.
  3. Clear this path from sys.path before importing modules. Not sure of a clean way to do this.

我个人希望将其删除并创建新的环境.很难知道您与该目录有多紧密联系,因此我会谨慎地假设其他软件包对其中的内容没有隐藏的依赖关系.

Personally, I'd want to delete it and create new envs. It can be hard to know how tied into this directory you are, so I'd be wary of assuming that other packages don't somehow have hidden dependencies on what is in there.

GitHub问题的推荐解决方法是添加以下环境变量,

The recommended workaround from the GitHub issue is to add the following environment variable,

export PYTHONNOUSERSITE=True

,可防止Conda加载其他本地site-packages目录.这样一来,您本来就不会遇到问题的.

which prevents Conda from loading other local site-packages directories. With this, you shouldn't have encountered the problem in the first place.

这篇关于Anaconda不使用激活环境中的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆