Python lưu trữ các gói cửa sổ ở đâu?
Các Mô-đun Python thường được lưu trữ trong /lib/site-packages trong thư mục Python của bạn. Nếu bạn muốn xem thư mục nào Python kiểm tra khi nhập mô-đun, bạn có thể đăng nhập như sau Show >>> import sys >>> print sys.path ['', 'C:\Python27', 'C:\Python27\Lib\site-packages', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\Python27\Scripts', 'C:\WINDOWS\SYSTEM32\python27.zip', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\site-packages\win32', 'C:\Python27\lib\site-packages\win32\lib', 'C:\Python27\lib\site-packages\Pythonwin'] Bạn cũng có thể liệt kê nơi các mô-đun riêng lẻ đang được tập lệnh sử dụng (hãy nói xin chào. py) nằm trên hệ thống bằng lệnh python -v. Ví dụ, $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ... Cập nhật ngày 01-Oct-2019 07. 05. 04 \(\newcommand{L}[1]{\. #1 \. }\newcommand{VL}[1]{\L{ \vec{#1} }}\newcommand{R}[1]{\operatorname{Re}\,(#1)}\newcommand{I}[1]Python tìm kiếm các mô-đun ở đâu?¶ Nhìn thấy
Giả sử chúng ta đã viết một mô-đun Python và lưu nó dưới dạng def func(): print("Running useful function")2, trong một thư mục có tên là def func(): print("Running useful function")3 Chúng tôi cũng có một tập lệnh có tên là def func(): print("Running useful function")4 trong một thư mục có tên là def func(): print("Running useful function")5 Chúng tôi muốn có thể def func(): print("Running useful function")6 mã trong def func(): print("Running useful function")2 để sử dụng trong def func(): print("Running useful function")4. Vì vậy, chúng tôi muốn có thể đưa dòng của anh ấy vào def func(): print("Running useful function")4 $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...0 Mô-đun và tập lệnh có thể trông như thế này Nội dung của $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...10 def func(): print("Running useful function") Nội dung của $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...11 $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...1 Hiện tại, def func(): print("Running useful function")4 sẽ thất bại với $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...6 Khi Python chạm vào dòng $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...13, nó sẽ cố gắng tìm một gói hoặc một mô-đun có tên là $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...14. Một gói là một thư mục chứa các mô-đun, nhưng bây giờ chúng tôi sẽ chỉ xem xét các mô-đun. Mô-đun là một tệp có phần mở rộng phù hợp, chẳng hạn như $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...15. Vì vậy, Python đang tìm kiếm một tệp def func(): print("Running useful function")2 và không tìm thấy nó Bạn sẽ thấy hiệu ứng tương tự tại bảng điều khiển Python tương tác hoặc trong IPython def func(): print("Running useful function")1 Python tìm kiếm các mô-đun trong “sys. đường dẫn"¶Python có một thuật toán đơn giản để tìm một mô-đun có tên đã cho, chẳng hạn như $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...14. Nó tìm kiếm một tệp có tên là def func(): print("Running useful function")2 trong các thư mục được liệt kê trong biến $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...19 def func(): print("Running useful function")5 Tệp def func(): print("Running useful function")2 nằm trong thư mục def func(): print("Running useful function")3 và thư mục này không có trong danh sách $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...19 Bởi vì $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...19 chỉ là một danh sách Python, giống như bất kỳ danh sách nào khác, chúng tôi có thể thực hiện thao tác nhập bằng cách thêm thư mục def func(): print("Running useful function")3 vào danh sách def func(): print("Running useful function")1 Có nhiều cách khác nhau để đảm bảo một thư mục luôn nằm trong danh sách $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...19 của Python khi bạn chạy Python, bao gồm
Là một bản hack thô sơ, bạn cũng có thể đặt thư mục def func(): print("Running useful function")3 của mình trên Python $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...19 ở đầu các tệp cần nó Nội dung của $ python -v hello.py PS C:\Users\Ayush> echo 'import scrapy' > hello.py PS C:\Users\Ayush> python -v hello.py # installing zipimport hook import zipimport # builtin # installed zipimport hook # C:\Python27\Lib\site.pyc matches C:\Python27\Lib\site.py import site # precompiled from C:\Python27\Lib\site.pyc # C:\Python27\Lib\os.pyc matches C:\Python27\Lib\os.py ... ...69 def func(): print("Running useful function")7 sau đó def func(): print("Running useful function")8 def func(): print("Running useful function")10 đơn giản ở trên sẽ chỉ hoạt động khi chạy tập lệnh từ thư mục chứa thư mục con def func(): print("Running useful function")3. Ví dụ def func(): print("Running useful function")1 Điều này là do thư mục def func(): print("Running useful function")3 mà chúng tôi đã chỉ định là một đường dẫn tương đối và do đó Python tìm thư mục def func(): print("Running useful function")3 trong thư mục làm việc hiện tại Đường dẫn gói Python của tôi ở đâu?Câu trả lời của bạn . Các thư mục gói trang web toàn cầu ("dist-gói") được liệt kê trong sys. con đường khi bạn chạy. trang web python -m. . Thư mục gói trang web cho mỗi người dùng (PEP 370) là nơi Python cài đặt các gói cục bộ của bạn. trang web python -m --user-site Các chương trình Python được lưu trữ ở đâu?Viết chương trình Python đầu tiên của bạn
. \ drive C:\ drive . Bạn sẽ lưu trữ tất cả các chương trình Python của mình trong thư mục này. |