Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lieryan opened this issue Jul 18, 2022 · 0 comments
Labels
stdlib Python modules in the Lib dir topic-venv Related to the venv module type-bug An unexpected behavior, bug, or error

Comments

@lieryan
Copy link

lieryan commented Jul 18, 2022

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

@lieryan lieryan added the type-bug An unexpected behavior, bug, or error label Jul 18, 2022
@lieryan lieryan changed the title On Windows, venv folders are unexpectedly importable due to how namespace package and venv works On Windows Powershell, venv folders are unexpectedly importable due to how namespace package and venv works Jul 18, 2022
@iritkatriel iritkatriel added stdlib Python modules in the Lib dir topic-venv Related to the venv module labels Nov 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-venv Related to the venv module type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants