問題描述
我注意到,當(dāng)我使用 conda 創(chuàng)建新環(huán)境時(shí),我可以在該環(huán)境中導(dǎo)入未安裝的 python 模塊.
I noticed that when I create a new enviornment with conda, I can import python modules in that environment that were NOT installed there.
以 keras 為例:雖然模塊不在那個(gè)環(huán)境中:
Example with keras: Although the module is NOT in that enviornment:
(py2) user@user-Precision-7920-Tower:~$ conda list keras
# packages in environment at /home/user/anaconda3/envs/py2:
#
# Name Version Build Channel
我仍然可以導(dǎo)入它,顯然是從系統(tǒng)(用戶)安裝,在 conda 之外!
I can still import it, apparently from the system (user) install, outside conda!
(py2) user@user-Precision-7920-Tower:~$ python
Python 2.7.15 | packaged by conda-forge | (default, Mar 5 2020, 14:56:06)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
>>> keras.__file__
'/home/user/.local/lib/python2.7/site-packages/keras/__init__.pyc'
其實(shí)conda里面的python是可以訪問非conda路徑的!
In fact, python inside conda has access to non-conda paths!
>>> import sys
>>>
>>> sys.stdout.write("
".join(sys.path))
/home/user/anaconda3/envs/py2/lib/python27.zip
/home/user/anaconda3/envs/py2/lib/python2.7
/home/user/anaconda3/envs/py2/lib/python2.7/plat-linux2
/home/user/anaconda3/envs/py2/lib/python2.7/lib-tk
/home/user/anaconda3/envs/py2/lib/python2.7/lib-old
/home/user/anaconda3/envs/py2/lib/python2.7/lib-dynload
/home/user/.local/lib/python2.7/site-packages <--
/home/user/anaconda3/envs/py2/lib/python2.7/site-packages>>>
Conda 應(yīng)該保持隔離.這條路是怎么走到這里的,如何避免這種情況發(fā)生?
Conda is supposed to keep things isolated. How did this path endd up in here, and how to avoid this from happening?
更新:
我的用戶級(jí) python 是 2.7,我注意到當(dāng)我使用 python 2.7 創(chuàng)建新的 conda 環(huán)境時(shí)總是會(huì)發(fā)生這種行為,這只是自動(dòng)將 .local/lib/python2.7/site-packages 添加到 PYTHONPATH.
My user-level python is 2.7, and I noticed this behavior always happen when I create a new conda environment with python 2.7, this just automatically adds the .local/lib/python2.7/site-packages to PYTHONPATH.
如果我使用 python3.x 創(chuàng)建新的 conda 環(huán)境,則不會(huì)發(fā)生這種情況.
If I create new conda environments with python3.x , this does not happen.
這是否意味著不能為與用戶級(jí) python 相同的 python 版本創(chuàng)建單獨(dú)的隔離 conda 環(huán)境?
Does this mean that one cannot create a separate isolated conda environment for the same python version as the user-level python?
推薦答案
除了 @VikashB 提到的之外,這些可能是使用 pip install --user
安裝的軟件包造成的.正如@TimRoberts 在評(píng)論中提到的那樣,填充 sys.path
變量的 site
模塊會(huì)搜索像 ~/.local/lib/python* 這樣的路徑/site-packages
默認(rèn)情況下.
In addition to what @VikashB mentioned, these can result from packages installed with pip install --user
. As @TimRoberts alluded to in the comments, the site
module, which populates the sys.path
variable, searches paths like ~/.local/lib/python*/site-packages
by default.
如果您出于某種原因需要保留這些包,則需要將它們移動(dòng)到非默認(rèn)位置,以便 site
模塊找不到它們.例如,
If you need to keep these packages for some reason, you'll need to move them to a non-default location so the site
module doesn't find them. For example,
mkdir ~/.local/lib/py_backup
mv ~/.local/lib/python* ~/.local/lib/py_backup
這將有效地隱藏它們,如果需要,它們?nèi)匀豢梢酝ㄟ^ PYTHONPATH 使用.
This will effectively hide them, and they could still be used through PYTHONPATH if necessary.
否則,只需刪除它們,即,
Otherwise, just remove them, i.e.,
rm -r ~/.local/lib/python*
作為參考,不鼓勵(lì) Conda 用戶使用 --user 標(biāo)志guide/tasks/manage-environments.html#pip-in-env" rel="nofollow noreferrer">Conda 文檔.Conda 環(huán)境假設(shè)環(huán)境完全隔離,因此 OP 報(bào)告等泄漏可能導(dǎo)致未定義的行為.
For reference, Conda users are discouraged from using the --user
flag in the Conda documentation. Conda environments assume full isolation of environments, so leakage such as OP reports can lead to undefined behavior.
這篇關(guān)于conda環(huán)境有訪問系統(tǒng)模塊,如何防止?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!