conda 环境可以访问系统模块,如何防止? [英] conda environment has access to system modules, how to prevent?

查看:62
本文介绍了conda 环境可以访问系统模块,如何防止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,当我使用 conda 创建新环境时,我可以在该环境中导入未安装在该环境中的 python 模块.

keras 示例:虽然模块不在那个环境中:

(py2) user@user-Precision-7920-Tower:~$ conda list keras# 在/home/user/anaconda3/envs/py2 环境中的包:## 命名版本构建频道

我仍然可以导入它,显然是从系统(用户)安装,在 conda 之外!

(py2) user@user-Precision-7920-Tower:~$ pythonPython 2.7.15 |由 conda-forge 打包 |(默认,2020 年 3 月 5 日,14:56:06)[GCC 7.3.0] 在 linux2 上输入帮助"、版权"、信用"或许可证"想要查询更多的信息.>>>进口keras使用 TensorFlow 后端.>>>keras.__file__'/home/user/.local/lib/python2.7/site-packages/keras/__init__.pyc'

其实conda里面的python是可以访问非conda路径的!

<预><代码>>>>导入系统>>>>>>sys.stdout.write("\n".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 应该保持隔离.这条路是怎么走到这里的,如何避免这种情况发生?

更新:

我的用户级 python 是 2.7,我注意到当我用 python 2.7 创建一个新的 conda 环境时总是会发生这种行为,这只会自动将 .local/lib/python2.7/site-packages 添加到 PYTHONPATH.

如果我使用 python3.x 创建新的 conda 环境,则不会发生这种情况.

这是否意味着不能为与用户级 python 相同的 python 版本创建单独的隔离 conda 环境?

解决方案

除了 @VikashB 提到的之外,这些可以由使用 pip install --user 安装的软件包产生.正如@TimRoberts 在评论中提到的那样,填充 sys.path 变量的 site 模块搜索类似 ~/.local/lib/python* 的路径/site-packages 默认情况下.

如果出于某种原因需要保留这些包,则需要将它们移动到非默认位置,以便 site 模块找不到它们.例如,

mkdir ~/.local/lib/py_backupmv ~/.local/lib/python* ~/.local/lib/py_backup

这将有效地隐藏它们,如果需要,它们仍然可以通过 PYTHONPATH 使用.

否则,只需删除它们,即

rm -r ~/.local/lib/python*

作为参考,不鼓励 Conda 用户使用 Conda 文档.Conda 环境假设环境完全隔离,因此诸如 OP 报告之类的泄漏可能会导致未定义的行为.

I noticed that when I create a new enviornment with conda, I can import python modules in that environment that were NOT installed there.

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

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'

In fact, python inside conda has access to non-conda paths!

>>> import sys
>>> 
>>> sys.stdout.write("\n".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 is supposed to keep things isolated. How did this path endd up in here, and how to avoid this from happening?

UPDATE:

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.

If I create new conda environments with python3.x , this does not happen.

Does this mean that one cannot create a separate isolated conda environment for the same python version as the user-level python?

解决方案

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.

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

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*

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.

这篇关于conda 环境可以访问系统模块,如何防止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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