It should be possible for a Python environment to disable the "user site" functionality without patching the stdlib
還沒有人認領這個 Issue。
評估
- 難度
- 5/5
- 預估耗時
- 一週以上
- 新手友好度
- 35/100
- Issue 類型
- 功能
- 描述清晰度
- 需要釐清
- 活躍度
- 停滯
- 技術堆疊
- python
研究方向
從 Lib/site.py 中 addusersitepackages 和 execsitecustomize 附近的啟動序列開始,接著檢查 venv 如何建立及設定環境。比較提議的 sitecustomize.py、usercustomize.py 與其他設定方法。完成的標準是:在不修改標準函式庫的情況下,提供一種按環境持久停用使用者 site 路徑的方法,同時對現有自訂維持相容的行為。
由索引模型根據 Issue 內容生成。
描述
The stdlib module site.py runs at startup, and is responsible for setting up a Python environment's basic search paths. There are various ways to customize this, e.g. you can drop in a sitecustomize.py file to add new search paths.
One thing site.py does by default is to add the "user site path" to sys.path, e.g. $HOME/.local/lib/python$VERSION/site-packages. This makes sense and is quite handy for global python installs where users want to add a few packages without the fuss of creating a venv: just pip install --user somepkg. And this is becoming more important as moves towards doing --user installs by default when outside a venv.
However, I think most people do not expect pip install --user somepkg will effectively inject somepkg into every venv that user ever creates. The whole point of creating a venv is to make an isolated environment. But, currently, site.py always adds the user site path to sys.path if possible, and there's no way to configure this through an envvar or sitecustomize.py – the only way to permanently disable it is to manually patch site.py in the stdlib. (Specifically: site.py runs addusersitepackages before it runs execsitecustomize, see here.) This leads to surprising behavior, or fragile hacks like modifying site.py with regexes.
There should be some way to create a Python environment where the user site path is disabled, without modifying the stdlib. (And venv should probably use it.)
One option would be to move execsitecustomize earlier in the setup, before addusersitepackages, so it has the option of doing import site; site.ENABLE_USER_SITE= False. I think the main downside here is that right now, if someone has a sitecustomize.py file in their user site path, it will no longer be executed. This is ... kind of fine? sitecustomize.py is supposed to be reserved for the python environment itself; user-specific customization is supposed to go in usercustomize.py. So e.g. this kind of setup is already broken if a distro ships a Python with a custom sitecustomize.py in it – which Ubuntu, for example, already does! And if it's a major problem we could even detect this case and warn about it. (E.g. after addusersitepackages make a second check for the existence of sitecustomize.py.)
Alternatively we could add some other kind of configuration file, earlysitecustomize.py or whatever. As long as it's something that's (a) on disk, so it's persistent for a given environment, and (b) can be dropped into a venv.
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 平均合併
- 1 天 9 小時
- 30 天內合併 PR
- 558
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python/cpython 的其他 Issue
-
docs pending
難度 2/5 1-3 小時 新手友好度 78/100
-
stdlib type-feature
難度 2/5 1-3 小時 新手友好度 78/100
-
stdlib type-feature
難度 2/5 1-3 小時 新手友好度 72/100
-
build type-bug
難度 2/5 1-3 小時 新手友好度 76/100
-
stdlib topic-email type-feature
難度 2/5 1-3 小時 新手友好度 70/100
相似的 Issue
-
難度 1/5 1 小時以內 新手友好度 90/100
-
bug
難度 2/5 1-3 小時 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
難度 2/5 1-3 小時 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
難度 2/5 1-3 小時 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
難度 2/5 1-3 小時 新手友好度 88/100
NousResearch/hermes-agent#117848 ·