問題描述
我有一個(gè) conda
環(huán)境,在一個(gè) bash 終端中,帶有一個(gè) Intel Python Distribution 解釋器.但是,在導(dǎo)入包時(shí),它們是從系統(tǒng)默認(rèn) Python 的用戶目錄中導(dǎo)入的,而不是從環(huán)境中導(dǎo)入的.查看 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
當(dāng)使用 base
環(huán)境時(shí),這不會(huì)發(fā)生;從正確的路徑導(dǎo)入包(例如 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
的相關(guān)部分(路徑中沒有顯式設(shè)置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
分別為 base
和 idp
環(huán)境生成這些 $PATH
:
Which yields these $PATH
s, 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
我做要導(dǎo)入的pandas
位于這里,應(yīng)該是:
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
中似乎有(或曾經(jīng)有)另一個(gè) Python 3.6,我懷疑 Conda 依賴解析器最終以某種方式將一些包解析為這個(gè)替代 site-packages
并且無意中將這個(gè)目錄包含在 sys.path
中.這似乎是一個(gè)已知問題.
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.
我相信這是因?yàn)?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
我希望這應(yīng)該顯示上面的目錄.
I expect that this should show the above directory.
由于據(jù)報(bào)道 PYTHONPATH
是空的(應(yīng)該是這樣!),因此不能對(duì)這種錯(cuò)誤加載負(fù)責(zé),因此我認(rèn)為是 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 環(huán)境未受影響這一事實(shí)可能是因?yàn)槟鸁o法跨不同的次要版本加載模塊.
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
- 從您的
PATH
中刪除該/home/torstein/.local/
.不過,這可能會(huì)導(dǎo)致其他問題.大概你在PATH
中有它,因?yàn)槟阌衅渌情_發(fā)的東西在那里. - 專門轉(zhuǎn)儲(chǔ)該
site-packages
目錄.在評(píng)論中,有人說這是不再使用的全局 Python 安裝的殘留物,因此擺脫它似乎是一件好事.不過,請(qǐng)備份它,以防有其他依賴項(xiàng). - 在導(dǎo)入模塊之前從
sys.path
中清除此路徑.不確定一種干凈的方式來做到這一點(diǎn).
- Remove that
/home/torstein/.local/
from yourPATH
. This could cause other issues though. Presumably you have it inPATH
because you have other non-development things in there. - 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. - Clear this path from
sys.path
before importing modules. Not sure of a clean way to do this.
就個(gè)人而言,我想刪除它并創(chuàng)建新的環(huán)境.可能很難知道你是如何與這個(gè)目錄綁定在一起的,所以我會(huì)謹(jǐn)慎地假設(shè)其他包對(duì)那里的內(nèi)容沒有隱藏的依賴關(guān)系.
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 問題推薦的解決方法是添加以下環(huán)境變量,
The recommended workaround from the GitHub issue is to add the following environment variable,
export PYTHONNOUSERSITE=True
這會(huì)阻止 Conda 加載其他本地 site-packages
目錄.有了這個(gè),你一開始就不應(yīng)該遇到這個(gè)問題.
which prevents Conda from loading other local site-packages
directories. With this, you shouldn't have encountered the problem in the first place.
這篇關(guān)于Anaconda 不使用激活環(huán)境中的包的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!