How to disable `site.ENABLE_USER_SITE` for the environment?

From the docs :

site.ENABLE_USER_SITE

A flag indicating the status of the directory of custom package sites. True means it is included and added to sys.path . False means that it was disabled at the user's request (using -s or PYTHONNOUSERSITE ). No one means that it has been disabled for security reasons (mismatch between the user or group identifier and the effective identifier) ​​or by the administrator.

I am particularly interested in the phrase or administrator. On the machines on which I am the administrator (for example, my own), how do I disable this option globally for a particular interpreter executable?

The reason I want to do this is because it is included in new conda environments: https://github.com/conda/conda/issues/448

+6
source share
1 answer

The value of this variable is fully defined in the 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 

The first test is just for the -s or the PYTHONNOUSERSITE environment variable used.

The only tests left are those that return None if the effective user or groupid is different from the userid or groupid process.

The administrator can set the effective user ID or bits of the group identifier , after which the effective user of the executable file will be changed to the owner or group of the executable file, and then the user executing Python, after which the above function will return None .

In addition, the sitecustomize.py package can again set to None and explicitly remove user directories from the path again. If so, the usercustomize.py import usercustomize.py skipped.

+6
source

Source: https://habr.com/ru/post/974587/


All Articles