我conda在bash终端中有一个使用Intel Python Distribution解释器的环境。但是,在导入软件包时,它们是从看起来像系统默认Python的用户目录而非环境的目录中导入的。查看版本差异和包的__spec__来源pandas。
conda
__spec__
pandas
~ $ 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)是从正确的路径导入的:
base
~ $ 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):
.bashrc
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
分别$PATH为base和idp环境产生这些s :
$PATH
idp
~ $ 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我 不 希望进口在这里,所在的位置应该是:
/home/torstein/anaconda3/envs/idp/lib/python3.6/site-packages/pandas
似乎有(或曾经有)另一个Python 3.6 PATH,而且我怀疑Conda依赖解析器最终以某种方式将某些软件包解析为该替代方案,site- packages并且无意中将该目录包含在中sys.path。这似乎是一个已知问题。
PATH
site- packages
sys.path
我相信这是因为pandas正在从此处加载模块:
/home/torstein/.local/lib/python3.6/site-packages/pandas
如果您签入Python
import sys sys.path
我希望这应该显示上面的目录。
由于据报道它PYTHONPATH是空的(应该是空的!),所以不能造成这种错误加载,因此我认为正是Conda以某种方式配置了env。
PYTHONPATH
另外,您的Python 3.7 env不受影响的事实很可能是因为您无法跨不同的次要版本加载模块。
您需要以某种方式摆脱这种依赖性。有几件事可以尝试
/home/torstein/.local/
site-packages
就个人而言,我想将其删除并创建新的环境。很难知道您与该目录有多紧密联系,因此我要谨慎地假设其他软件包对其中的内容没有隐藏的依赖性。
GitHub问题的建议解决方法是添加以下环境变量,
export PYTHONNOUSERSITE=True
这样可以防止Conda加载其他本地site-packages目录。这样一来,您本来就不会遇到问题。