On Windows Powershell, venv folders are unexpectedly importable due to how namespace package and venv works

オープン
#94,957 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
powershell, python

調査の方向性

まず、報告にある Windows PowerShell コマンドを再現し、venv を作成してアクティベートした後の sys.path を調べます。Windows と Linux の venv の動作を比較し、その後、Include、Lib、Scripts ディレクトリがどのようにインポート可能になるのかを特定します。通常の環境のインポートを壊すことなく、意図しない venv ディレクトリがインポートできなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

stdlib topic-venv type-bug

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

主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。