python / python/cpython

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

Aperta
#94,957 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-venv type-bug
Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 9h
PR unite (30g)
558

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo i comandi Windows PowerShell riportati nel report e ispeziona sys.path dopo aver creato e attivato una venv. Confronta il comportamento della venv su Windows e Linux, quindi determina come le directory Include, Lib e Scripts diventano importabili. Il lavoro è completato quando le directory venv indesiderate non sono più importabili senza compromettere le normali importazioni dell’ambiente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
powershell, python
Ambito
operating-systems, tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.