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

Đang mở
#94,957 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
powershell, python
Lĩnh vực
operating-systems, tooling

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện các lệnh Windows PowerShell trong báo cáo và kiểm tra sys.path sau khi tạo và kích hoạt một venv. So sánh hành vi của venv trên Windows và Linux, sau đó xác định cách các thư mục Include, Lib và Scripts trở nên có thể import. Hoàn thành khi các thư mục venv không chủ ý không còn có thể import mà không làm hỏng các import thông thường của môi trường.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.