On Windows Powershell, venv folders are unexpectedly importable due to how namespace package and venv works
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- 平均合併
- 1 天 9 小時
- 30 天內合併 PR
- 558
描述
Bug report
PEP420 introduced implicit namespace package, basically by creating a folder without __init__.py, it is treated as namespace package.
For some reasons, in Windows Powershell, venv added the environment's root folder into sys.path (this behavior was not observed in Linux's venv), e.g.
PS C:\Users\TestUser> python -m venv testenv
PS C:\Users\TestUser> . .\testenv\Scripts\activate
(testenv) PS C:\Users\TestUser> python
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
[
[snip]
'C:\\Users\\TestUser\\testenv',
'C:\\Users\\TestUser\\testenv\\lib\\site-packages'
]
And because of the way namespace package works, you can import some folders that really aren't intended to be importable:
>>> import Include
>>> Include.__path__
_NamespacePath(['C:\\Users\\TestUser\\testenv\\Include'])
>>>
This was a namespace package created from the Include folder in the root of the testvenv folder:
PS C:\Users\TestUser> ls .\testenv\
Directory: C:\Users\TestUser\testenv
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 7/18/2022 1:15 PM Include
d----- 7/18/2022 1:15 PM Lib
d----- 7/18/2022 1:15 PM Scripts
-a---- 7/18/2022 1:15 PM 127 pyvenv.cfg
Similarly, you could have imported Lib and Scripts.
Your environment
Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先重現報告中的 Windows PowerShell 命令,並在建立及啟用 venv 後檢查 sys.path。比較 Windows 和 Linux 上 venv 的行為,然後判斷 Include、Lib 和 Scripts 目錄如何變得可匯入。當不再能夠匯入非預期的 venv 目錄,同時又不會破壞正常的環境匯入時,即表示完成。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- powershell, python
- 領域
- operating-systems, tooling
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100