On Windows Powershell, venv folders are unexpectedly importable due to how namespace package and venv works
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 36k
- Merge moyen
- 1 j 9 h
- PR mergées (30 j)
- 558
Description
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
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par reproduire les commandes Windows PowerShell du rapport et inspectez sys.path après avoir créé et activé une venv. Comparez le comportement de la venv sous Windows et Linux, puis déterminez comment les répertoires Include, Lib et Scripts deviennent importables. La tâche est terminée lorsque les répertoires venv indésirables ne sont plus importables sans perturber les importations normales de l’environnement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- powershell, python
- Domaine
- operating-systems, tooling
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100