如何为环境禁用`site.ENABLE_USER_SITE`?

时间:2014-08-30 16:02:14

标签: python

来自the docs

  

site.ENABLE_USER_SITE

     

显示用户site-packages目录状态的标志。 True表示已启用并已添加到sys.pathFalse表示已被用户请求(-sPYTHONNOUSERSITE)禁用。无意味着出于安全原因(用户或组ID与有效ID不匹配)或管理员而被禁用。

我对短语或管理员特别感兴趣。在我是管理员(即我自己的)的机器上,如何针对特定的解释器可执行文件全局禁用此选项?

我想这样做的原因是新的conda环境会启用此功能:https://github.com/conda/conda/issues/448

2 个答案:

答案 0 :(得分:7)

该变量的值完全由Python code确定:

def check_enableusersite():
    """Check if user site directory is safe for inclusion

    The function tests for the command line flag (including environment var),
    process uid/gid equal to effective uid/gid.

    None: Disabled for security reasons
    False: Disabled by user (command line option)
    True: Safe and enabled
    """
    if sys.flags.no_user_site:
        return False

    if hasattr(os, "getuid") and hasattr(os, "geteuid"):
        # check process uid == effective uid
        if os.geteuid() != os.getuid():
            return None
    if hasattr(os, "getgid") and hasattr(os, "getegid"):
        # check process gid == effective gid
        if os.getegid() != os.getgid():
            return None

    return True

第一个测试仅适用于-s开关或已使用的PYTHONNOUSERSITE环境变量。

如果有效用户标识或组ID与进程用户标识或组ID不同,则返回None的测试仍然存在。

管理员可以set the effective user id or group id bits,此时可执行文件的有效用户更改为可执行文件的所有者或组,而不是执行Python的用户,此时上述函数将返回{{1} }。

除此之外,None包可以再次将值设置为sitecustomize.py,并再次从路径中显式删除用户目录。如果是,则跳过None导入步骤。

答案 1 :(得分:0)

基本上你可以添加环境变量PYTHONNOUSERSITE=1(只需将其设置为某些内容)

因此,如果需要全局更改它,请将其添加到 .bashrc 或您使用的任何内容中。

对于康达:

您可以指定将为特定环境设置的变量 see link 简而言之:

conda env config vars set PYTHONNOUSERSITE=1

然后重新激活您的环境