Python nhập pyc từ đường dẫn

Python cho phép bạn nhập mã từ tệp ZIP trực tiếp thông qua nhập Zip. Tính năng tích hợp thú vị này cho phép bạn nén mã Python cho mục đích phân phối. Nhập zip cũng hữu ích nếu bạn thường xuyên làm việc với mã Python có trong tệp ZIP. Trong cả hai trường hợp, học cách tạo các tệp ZIP có thể nhập và nhập mã từ chúng sẽ là một kỹ năng có giá trị

Show

Ngay cả khi quy trình làm việc hàng ngày của bạn không liên quan đến các tệp ZIP chứa mã Python, bạn vẫn sẽ học được một số kỹ năng mới thú vị và thú vị bằng cách khám phá nhập Zip thông qua hướng dẫn này

Trong hướng dẫn này, bạn sẽ học

  • Nhập Zip là gì
  • Khi nào nên sử dụng nhập Zip trong mã của bạn
  • Cách tạo tệp ZIP có thể nhập bằng
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
    ..     zip_module.writepy("hello.py")
    ...
    
    >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
    ..     zip_module.printdir()
    ...
    File Name                                             Modified             Size
    hello.pyc                                      2021-10-18 05:40:04          313
    
    9
  • Cách cung cấp các tệp ZIP của bạn để nhập mã từ chúng

Bạn cũng sẽ học cách sử dụng mô-đun

hello/
|
├── __init__.py
└── hello.py
0 để tự động nhập mã từ các tệp ZIP mà không cần thêm chúng vào đường dẫn tìm kiếm mô-đun của Python. Để làm điều này, bạn sẽ viết mã một hệ thống plugin tối thiểu tải mã Python từ các tệp ZIP

Để tận dụng tối đa hướng dẫn này, bạn nên có kiến ​​thức trước đó về cách hệ thống nhập của Python hoạt động. Bạn cũng nên biết kiến ​​thức cơ bản về thao tác tệp ZIP với

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
9, làm việc với tệp và sử dụng câu lệnh
hello/
|
├── __init__.py
└── hello.py
2

Tiền thưởng miễn phí. 5 Suy nghĩ về Làm chủ Python, một khóa học miễn phí dành cho các nhà phát triển Python cho bạn thấy lộ trình và tư duy mà bạn sẽ cần để đưa các kỹ năng Python của mình lên một tầm cao mới

Tìm hiểu nhập mã zip Python

Kể từ Python, bạn có thể nhập các mô-đun và gói từ bên trong. Tính năng này được gọi là nhập Zip và khá hữu ích khi bạn cần phân phối một gói hoàn chỉnh dưới dạng một tệp duy nhất, đây là trường hợp sử dụng phổ biến nhất của tính năng này

PEP 273 đã giới thiệu tính năng nhập Zip dưới dạng tính năng tích hợp. Tính năng này đã được chấp nhận rộng rãi như một tính năng bắt buộc phải có trong cộng đồng Python vì việc phân phối một số tệp

hello/
|
├── __init__.py
└── hello.py
3,
hello/
|
├── __init__.py
└── hello.py
4 và
hello/
|
├── __init__.py
└── hello.py
5 riêng biệt không phải lúc nào cũng phù hợp và hiệu quả

Nhập zip có thể đơn giản hóa quá trình chia sẻ và phân phối mã của bạn để đồng nghiệp và người dùng cuối của bạn không phải loay hoay tìm cách giải nén các tệp vào đúng vị trí để mã hoạt động

Ghi chú. Phần mở rộng tệp

hello/
|
├── __init__.py
└── hello.py
5 không còn được sử dụng kể từ. Xem PEP 488 để biết chi tiết

PEP 302 đã thêm một loạt móc nhập cung cấp hỗ trợ tích hợp cho nhập Zip. Nếu bạn muốn nhập các mô-đun và gói từ tệp ZIP, thì bạn chỉ cần tệp xuất hiện trong Python

Đường dẫn tìm kiếm mô-đun là danh sách các thư mục và tệp ZIP. Nó sống ở. Python tự động tìm kiếm thông qua các mục trong danh sách này khi bạn chạy câu lệnh

hello/
|
├── __init__.py
└── hello.py
8 trong mã của mình

Trong các phần sau, bạn sẽ tìm hiểu cách tạo các tệp ZIP sẵn sàng để nhập bằng các công cụ và kỹ thuật Python khác nhau. Bạn cũng sẽ tìm hiểu về một số cách để thêm các tệp đó vào đường dẫn tìm kiếm mô-đun Python hiện tại của bạn. Cuối cùng, bạn sẽ tìm hiểu về

hello/
|
├── __init__.py
└── hello.py
0, mô-đun hỗ trợ tính năng nhập Zip phía sau hậu trường

Loại bỏ các quảng cáo

Tạo tệp ZIP có thể nhập của riêng bạn

Nhập zip cho phép bạn nhanh chóng phân phối mã được tổ chức trên một số mô-đun và gói dưới dạng một tệp. Python đã bảo vệ bạn khi tạo các tệp ZIP có thể nhập. Mô-đun

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
9 từ thư viện tiêu chuẩn bao gồm một lớp được gọi để thao tác các tệp ZIP. Nó cũng bao gồm một lớp chuyên biệt hơn được gọi là , tạo điều kiện thuận lợi cho việc tạo các tệp ZIP có thể nhập được

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
2 cho phép bạn gộp mã Python vào các tệp ZIP một cách nhanh chóng và hiệu quả. Lớp kế thừa từ
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
1, vì vậy nó chia sẻ cùng một giao diện cơ sở. Tuy nhiên, có hai điểm khác biệt chính giữa các lớp này

  1. Trình khởi tạo của
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
    ..     zip_pkg.writepy("hello")
    ...
    
    >>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
    ..     zip_pkg.printdir()
    ...
    File Name                                             Modified             Size
    hello/__init__.pyc                             2021-10-18 05:56:00          110
    hello/hello.pyc                                2021-10-18 05:56:00          319
    
    2 nhận một đối số tùy chọn có tên là
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
    ..     zip_pkg.writepy("hello")
    ...
    
    >>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
    ..     zip_pkg.printdir()
    ...
    File Name                                             Modified             Size
    hello/__init__.pyc                             2021-10-18 05:56:00          110
    hello/hello.pyc                                2021-10-18 05:56:00          319
    
    6, cho phép bạn tối ưu hóa mã Python bằng cách biên dịch mã thành trước khi lưu trữ mã
  2. Lớp
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
    ..     zip_pkg.writepy("hello")
    ...
    
    >>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
    ..     zip_pkg.printdir()
    ...
    File Name                                             Modified             Size
    hello/__init__.pyc                             2021-10-18 05:56:00          110
    hello/hello.pyc                                2021-10-18 05:56:00          319
    
    2 cung cấp một phương thức gọi là , chấp nhận một mô-đun hoặc gói Python làm đối số và thêm nó vào tệp ZIP đích

Nếu

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
6 là
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
0, giá trị mặc định của nó, thì tệp
hello/
|
├── __init__.py
└── hello.py
3 đầu vào sẽ tự động được biên dịch thành tệp
hello/
|
├── __init__.py
└── hello.py
4 và sau đó được thêm vào kho lưu trữ đích. Lý do tại sao điều này xảy ra? . Bạn sẽ tìm hiểu thêm về chủ đề này trong các phần tiếp theo

Trong hai phần sau, bạn sẽ bắt đầu tạo các tệp ZIP có thể nhập của riêng mình chứa các mô-đun và gói

Gói các mô-đun Python vào tệp ZIP

Trong phần này, bạn sẽ sử dụng

>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
5 để biên dịch tệp
hello/
|
├── __init__.py
└── hello.py
3 thành mã byte và thêm tệp kết quả
hello/
|
├── __init__.py
└── hello.py
4 vào kho lưu trữ ZIP. Để dùng thử
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8, giả sử bạn có mô-đun
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9

"""Print a greeting message."""
# hello.py

def greet(name="World"):
    print(f"Hello, {name}! Welcome to Real Python!")

Mô-đun này định nghĩa một hàm có tên là

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
0 nhận vào ____28_______1 làm đối số và in một thông báo lời chào thân thiện ra màn hình

Bây giờ, giả sử bạn muốn đóng gói mô-đun này thành một tệp ZIP mà bạn có thể nhập sau. Để làm điều này, bạn có thể chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313

Sau khi chạy mã này, bạn sẽ có tệp

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 trong thư mục làm việc hiện tại của mình. Cuộc gọi đến
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8 trên
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
4 sẽ tự động biên dịch ____17_______9 thành ____28_______6 và lưu trữ nó trong tệp ZIP cơ bản, ____28_______2. Đó là lý do tại sao hiển thị
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
6 thay vì tệp
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 ban đầu của bạn. Quá trình biên dịch tự động này đảm bảo quy trình nhập hiệu quả

Ghi chú. Theo mặc định, lớp

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
2 không nén các mô-đun và gói Python của bạn. Nó chỉ lưu trữ chúng trong một thùng chứa tệp ZIP. Nếu bạn muốn nén các tệp nguồn của mình, bạn cần cung cấp rõ ràng một phương pháp nén thông qua đối số
>>> import sys

>>> sys.path.append("/path/to/hello.zip")

>>> # The hello.zip file is at the end of sys.path
>>> sys.path[-1]
'/path/to/hello.zip'

>>> from hello import greet
>>> greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 của
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
2. Hiện tại, Python hỗ trợ các phương thức nén Deflate, bzip2 và LZMA

Trong hướng dẫn này, bạn sẽ dựa vào giá trị mặc định của

>>> import sys

>>> sys.path.append("/path/to/hello.zip")

>>> # The hello.zip file is at the end of sys.path
>>> sys.path[-1]
'/path/to/hello.zip'

>>> from hello import greet
>>> greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2, , có nghĩa là các tệp nguồn của bạn sẽ không bị nén. Việc nén các tệp nguồn có thể ảnh hưởng đến hiệu suất của các thao tác nhập, bạn sẽ tìm hiểu sau trong hướng dẫn này

Bạn cũng có thể đóng gói thủ công các tệp

hello/
|
├── __init__.py
└── hello.py
3 và
hello/
|
├── __init__.py
└── hello.py
4 thành tệp ZIP bằng cách sử dụng bất kỳ trình lưu trữ tệp thông thường nào. Nếu kho lưu trữ kết quả chứa các tệp
hello/
|
├── __init__.py
└── hello.py
3 mà không có tệp
hello/
|
├── __init__.py
└── hello.py
4 tương ứng, thì Python sẽ biên dịch chúng trong lần đầu tiên bạn nhập từ tệp ZIP cụ thể đó

Python sẽ không sửa đổi tệp ZIP bên dưới để thêm các tệp

hello/
|
├── __init__.py
└── hello.py
4 mới được biên dịch. Vì vậy, lần sau khi bạn chạy nhập, Python sẽ biên dịch lại mã. Hành vi này sẽ làm cho quá trình nhập chậm hơn

Bạn cũng có thể chuyển một thư mục làm đối số đầu tiên cho

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8. Nếu thư mục đầu vào không phải là một gói Python, thì phương thức này sẽ quét nó để tìm các tệp
hello/
|
├── __init__.py
└── hello.py
3, biên dịch chúng thành các tệp
hello/
|
├── __init__.py
└── hello.py
4 và thêm các tệp
hello/
|
├── __init__.py
└── hello.py
4 đó ở cấp cao nhất của tệp ZIP đích. Bước quét không phải là đệ quy, có nghĩa là các thư mục con không được quét để tìm tệp nguồn

Bạn có thể điều chỉnh thêm quá trình biên dịch bằng cách đặt đối số

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
6 của
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
2 thành một trong các giá trị sau

ValueOptimizations

$ export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"
7Không thực hiện bất kỳ tối ưu hóa nào
$ export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"
8Xóa
$ export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"
9 câu lệnh
# .bashrc

if [ -f /path/to/hello.zip ]; then
    export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"
fi
0Xóa
$ export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"
9 câu lệnh và chuỗi tài liệu

Với những giá trị này, bạn có thể tinh chỉnh mức độ tối ưu hóa mà bạn muốn sử dụng khi

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8 biên dịch tệp
hello/
|
├── __init__.py
└── hello.py
3 của bạn thành tệp
hello/
|
├── __init__.py
└── hello.py
4 trước khi lưu trữ chúng

Cho đến giờ, bạn đã học cách gộp một hoặc nhiều mô-đun vào một tệp ZIP. Trong mã hóa hàng ngày của bạn, bạn cũng có thể cần nén một gói Python hoàn chỉnh. Bạn sẽ học cách làm điều đó trong phần sau

Loại bỏ các quảng cáo

Gộp các gói Python vào tệp ZIP

Bạn cũng có thể gộp các gói Python vào các tệp ZIP bằng cách sử dụng phương thức

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
2 và phương thức
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8 của nó. Như bạn đã biết, nếu bạn chuyển một thư mục thông thường làm đối số đầu tiên cho
>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8, thì phương thức này sẽ quét thư mục để tìm các tệp
hello/
|
├── __init__.py
└── hello.py
3, biên dịch chúng và thêm các tệp
hello/
|
├── __init__.py
└── hello.py
4 tương ứng vào tệp ZIP kết quả

Mặt khác, nếu thư mục đầu vào là một gói Python, thì

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8 sẽ biên dịch tất cả các tệp
hello/
|
├── __init__.py
└── hello.py
3 và thêm chúng vào tệp ZIP, giữ nguyên cấu trúc bên trong của gói

Để dùng thử

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8 với gói Python, hãy tạo một thư mục
>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]
3 mới và sao chép tệp
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 của bạn vào đó. Sau đó, thêm một mô-đun
>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]
5 trống để biến thư mục thành một gói. Bạn nên kết thúc với cấu trúc sau

hello/
|
├── __init__.py
└── hello.py

Bây giờ, giả sử rằng bạn muốn gói gói này thành tệp ZIP cho mục đích phân phối. Nếu đúng như vậy, thì bạn có thể chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319

Cuộc gọi đến

>>> import zipfile

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="w") as zip_pkg:
..     zip_pkg.writepy("hello")
...

>>> with zipfile.PyZipFile("hello_pkg.zip", mode="r") as zip_pkg:
..     zip_pkg.printdir()
...
File Name                                             Modified             Size
hello/__init__.pyc                             2021-10-18 05:56:00          110
hello/hello.pyc                                2021-10-18 05:56:00          319
8 lấy gói ________70_____7 làm đối số, tìm kiếm các tệp ________1_____3 bên trong nó, biên dịch chúng vào các tệp ________1_____4 và cuối cùng thêm chúng vào tệp zip đích, giữ cùng một cấu trúc gói

Hiểu những hạn chế của nhập Zip

Khi bạn sử dụng các tệp ZIP để phân phối mã Python, bạn cần xem xét một số hạn chế của việc nhập Zip

  • Không thể tải các tệp động, chẳng hạn như
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
    ..     zip_module.writepy("hello.py")
    ...
    
    >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
    ..     zip_module.printdir()
    ...
    File Name                                             Modified             Size
    hello.pyc                                      2021-10-18 05:40:04          313
    
    00,
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
    ..     zip_module.writepy("hello.py")
    ...
    
    >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
    ..     zip_module.printdir()
    ...
    File Name                                             Modified             Size
    hello.pyc                                      2021-10-18 05:40:04          313
    
    01 và
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
    ..     zip_module.writepy("hello.py")
    ...
    
    >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
    ..     zip_module.printdir()
    ...
    File Name                                             Modified             Size
    hello.pyc                                      2021-10-18 05:40:04          313
    
    02
  • Nhập mã từ tệp
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    3 ngụ ý ảnh hưởng đến hiệu suất
  • Nhập mã từ tệp nén sẽ không thành công nếu không có thư viện giải nén

Bạn có thể bao gồm bất kỳ loại tệp nào trong kho lưu trữ ZIP của mình. Tuy nhiên, khi người dùng của bạn nhập mã từ các kho lưu trữ này, chỉ các tệp

hello/
|
├── __init__.py
└── hello.py
3,
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
05,
hello/
|
├── __init__.py
└── hello.py
4 và
hello/
|
├── __init__.py
└── hello.py
5 được đọc. Không thể nhập mã từ các tệp động, chẳng hạn như
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
00,
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
01 và
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
02 nếu chúng nằm trong tệp ZIP. Ví dụ: bạn không thể tải các thư viện dùng chung và mô-đun mở rộng được viết bằng C từ kho lưu trữ ZIP

Bạn có thể khắc phục hạn chế này bằng cách trích xuất các mô-đun động từ các tệp ZIP của mình, ghi chúng vào hệ thống tệp, sau đó tải mã của chúng. Tuy nhiên, điều đó có nghĩa là bạn cần tạo các tệp tạm thời và xử lý các lỗi và rủi ro bảo mật có thể xảy ra, điều này có thể làm phức tạp mọi thứ

Nhập mã zip cũng có thể ám chỉ sự thỏa hiệp về hiệu suất, như bạn đã biết trước đó trong hướng dẫn này. Nếu kho lưu trữ của bạn chứa

hello/
|
├── __init__.py
└── hello.py
3 mô-đun, thì Python sẽ biên dịch chúng để đáp ứng quá trình nhập. Tuy nhiên, nó sẽ không lưu các tệp
hello/
|
├── __init__.py
└── hello.py
4 tương ứng. Hành vi này có thể làm giảm hiệu suất của hoạt động nhập khẩu

Cuối cùng, nếu bạn cần nhập mã từ tệp ZIP đã nén, thì ______0_______13 phải có sẵn trong môi trường làm việc của bạn cho mục đích giải nén. Nhập mã từ kho lưu trữ nén không thành công với thông báo thiếu

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
13 nếu thư viện này không khả dụng. Ngoài ra, bước giải nén bổ sung thêm chi phí hoạt động cho quy trình nhập. Vì những lý do này, bạn sẽ sử dụng các tệp ZIP không nén trong hướng dẫn này

Nhập mã Python từ tệp ZIP

Cho đến thời điểm này, bạn đã học cách tạo các tệp ZIP có thể nhập của riêng mình cho mục đích phân phối. Bây giờ, giả sử rằng bạn đang ở đầu bên kia và bạn đang nhận các tệp ZIP với các gói và mô-đun Python. Làm cách nào bạn có thể nhập mã từ chúng?

Để Python nhập mã từ tệp ZIP, tệp đó phải có sẵn trong đường dẫn tìm kiếm mô-đun của Python, được lưu trữ trong

hello/
|
├── __init__.py
└── hello.py
7. Biến cấp mô-đun này chứa danh sách các chuỗi chỉ định đường dẫn tìm kiếm cho các mô-đun. Nội dung của
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
16 bao gồm

  • Thư mục chứa tập lệnh mà bạn đang chạy
  • Thư mục hiện tại, nếu bạn đã chạy trình thông dịch tương tác
  • Các thư mục trong biến môi trường, nếu được đặt
  • Một danh sách thư mục phụ thuộc vào cài đặt Python cụ thể của bạn
  • Các thư mục được liệt kê trong bất kỳ tệp cấu hình đường dẫn nào (tệp ______0_______18)

Bảng sau đây chỉ ra một số cách để thêm các tệp ZIP của bạn vào

hello/
|
├── __init__.py
└── hello.py
7

Tùy chọn Mã mục tiêu hoặc Trình thông dịchCác phương thức

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
20,
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
21 và
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
22Mã Python mà bạn đang viết và chạyBiến môi trường
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17Mọi trình thông dịch Python mà bạn chạy trên hệ thống của mìnhTệp cấu hình đường dẫn Python hoặc tệp
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18Trình thông dịch Python có chứa tệp
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18

Trong các phần sau, bạn sẽ khám phá ba cách này để thêm các mục vào

hello/
|
├── __init__.py
└── hello.py
7 để bạn có thể cung cấp các tệp ZIP của mình để nhập nội dung của chúng

Loại bỏ các quảng cáo

Sử dụng hello/ | ├── __init__.py └── hello.py 7 một cách linh hoạt để nhập Zip

Bởi vì

hello/
|
├── __init__.py
└── hello.py
7 là một đối tượng
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
29, bạn có thể thao tác nó từ mã Python của mình bằng cách sử dụng các phương thức
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
29 thông thường. Nói chung, để thêm các mục mới vào đối tượng
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
29, bạn có thể sử dụng
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
32,
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
33 hoặc
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
34

Thông thường, bạn sẽ sử dụng

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
35 để thêm các mục mới vào
hello/
|
├── __init__.py
└── hello.py
7 từ mã Python của mình. Gọi
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
32 theo cách này sẽ chèn
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
38 vào đầu danh sách, đảm bảo rằng mục mới thêm của bạn được ưu tiên hơn mục hiện có. Có
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
38 ngay từ đầu cho phép bạn theo dõi các mô-đun và gói hiện có khi có thể xảy ra xung đột tên

Bây giờ, giả sử bạn cần thêm tệp

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 chứa mô-đun
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 của bạn vào
hello/
|
├── __init__.py
└── hello.py
7 của Python hiện tại. Trong trường hợp này, bạn có thể chạy mã trong ví dụ bên dưới. Lưu ý rằng để ví dụ này hoạt động trên máy của bạn, bạn cần cung cấp đường dẫn chính xác đến
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2

>>>

>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!

Khi bạn đã thêm đường dẫn đến

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 vào
hello/
|
├── __init__.py
└── hello.py
7 của mình, thì bạn có thể nhập các đối tượng từ
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 như bạn làm với bất kỳ mô-đun thông thường nào

Nếu, chẳng hạn như

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
47, tệp ZIP của bạn chứa gói Python, thì bạn cũng có thể thêm gói đó vào
hello/
|
├── __init__.py
└── hello.py
7. Trong trường hợp này, việc nhập khẩu phải tương đối theo gói

>>>

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!

Vì mã của bạn hiện đang ở trong một gói nên bạn cần nhập mô-đun

>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]
7 từ gói
>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]
7. Sau đó, bạn có thể truy cập chức năng
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
0 như bình thường

Một tùy chọn khác để thêm các mục vào

hello/
|
├── __init__.py
└── hello.py
7 là sử dụng
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
33. Phương thức này lấy một đối tượng làm đối số và thêm nó vào cuối danh sách bên dưới. Khởi động lại phiên tương tác Python của bạn và chạy mã cung cấp đường dẫn đến
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2

>>>

>>> import sys

>>> sys.path.append("/path/to/hello.zip")

>>> # The hello.zip file is at the end of sys.path
>>> sys.path[-1]
'/path/to/hello.zip'

>>> from hello import greet
>>> greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!

Kỹ thuật này hoạt động tương tự như sử dụng

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
32. Tuy nhiên, đường dẫn đến tệp ZIP của bạn hiện ở cuối
hello/
|
├── __init__.py
└── hello.py
7. Nếu bất kỳ mục nào trước đó trong danh sách chứa một mô-đun có tên là
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9, thì Python sẽ nhập từ mô-đun đó thay vì từ mô-đun
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 mới được thêm vào của bạn

Bạn cũng có thể sử dụng

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
33 trong một vòng lặp để thêm một số tệp vào
hello/
|
├── __init__.py
└── hello.py
7 hoặc bạn chỉ có thể sử dụng
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
34. Phương pháp này lấy một mục có thể lặp lại và thêm nội dung của nó vào cuối danh sách cơ bản. Như với
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
33, hãy nhớ rằng
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
34 sẽ thêm các tệp của bạn vào cuối
hello/
|
├── __init__.py
└── hello.py
7, vì vậy các tên hiện có có thể ẩn các mô-đun và gói trong tệp ZIP của bạn

Sử dụng >>> import zipfile >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module: .. zip_module.writepy("hello.py") ... >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module: .. zip_module.printdir() ... File Name Modified Size hello.pyc 2021-10-18 05:40:04 313 17 để nhập mã zip trên toàn hệ thống

Trong một số trường hợp, bạn có thể cần có một tệp ZIP nhất định để nhập nội dung của tệp đó từ bất kỳ tập lệnh hoặc chương trình nào mà bạn chạy trên máy tính của mình. Trong những trường hợp này, bạn có thể sử dụng biến môi trường

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 để làm cho Python tự động tải kho lưu trữ của bạn vào
hello/
|
├── __init__.py
└── hello.py
7 bất cứ khi nào bạn chạy trình thông dịch

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 sử dụng cùng định dạng với biến môi trường
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
69, một danh sách các đường dẫn thư mục được phân tách bằng. Trên các hệ thống Unix, chẳng hạn như Linux và macOS, hàm này trả về dấu hai chấm (
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
71), trong khi trên Windows, hàm này trả về dấu chấm phẩy (
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
72)

Ví dụ: nếu bạn đang dùng Linux hoặc macOS, thì bạn có thể thêm tệp

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 của mình vào
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 bằng cách chạy lệnh sau

$ export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"

Lệnh này thêm

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
75 vào
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 hiện tại của bạn và xuất nó để nó có sẵn trong phiên cuối hiện tại

Ghi chú. Lệnh trên xuất một phiên bản tùy chỉnh của

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 bao gồm đường dẫn đến
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2. Phiên bản biến tùy chỉnh này chỉ khả dụng trong phiên dòng lệnh hiện tại của bạn và sẽ bị mất sau khi bạn đóng phiên

Nếu bạn đang chạy Bash dưới dạng trình bao hiện tại, thì bạn có thể cung cấp phiên bản tùy chỉnh này của

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 cho tất cả các phiên dòng lệnh của mình bằng cách thêm mã sau vào tệp ________0____80 của bạn

# .bashrc

if [ -f /path/to/hello.zip ]; then
    export PYTHONPATH="$PYTHONPATH:/path/to/hello.zip"
fi

Mã này kiểm tra xem

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 có tồn tại trên hệ thống tệp của bạn không. Nếu vậy, nó sẽ thêm tệp vào biến
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 và xuất nó. Bởi vì Bash chạy tệp này mỗi khi bạn khởi chạy một phiên bản dòng lệnh mới, nên tùy chỉnh
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 sẽ có sẵn trong mỗi phiên

Bây giờ bạn có thể ra lệnh

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
84 để chạy trình thông dịch. Khi bạn ở đó, hãy kiểm tra nội dung của
hello/
|
├── __init__.py
└── hello.py
7 như bình thường

>>>

>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]

Mát lạnh. Tệp

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 của bạn có trong danh sách. Từ thời điểm này, bạn sẽ có thể nhập các đối tượng từ
>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 như bạn đã làm ở phần trên. Đi trước và cung cấp cho nó một thử

Một điểm quan trọng cần lưu ý trong đầu ra ở trên là tệp

>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 của bạn không ở đầu
hello/
|
├── __init__.py
└── hello.py
7, điều này ngụ ý rằng một mô-đun cùng tên xuất hiện trước đó sẽ được ưu tiên hơn mô-đun
>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]
7 của bạn, theo cách Python xử lý nó

Để thêm một mục vào

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 trên hệ thống Windows, bạn có thể thực hiện một lệnh trong cửa sổ
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
92 của mình

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
0

Lệnh này thêm

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
93 vào nội dung hiện tại của biến
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 trên máy Windows của bạn. Để kiểm tra, hãy chạy trình thông dịch Python trong cùng một phiên nhắc lệnh và xem nội dung của
hello/
|
├── __init__.py
└── hello.py
7, như bạn đã làm trước đây

Ghi chú. Một lần nữa, biến

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 mà bạn đã đặt bằng lệnh trên sẽ chỉ khả dụng trong phiên cuối hiện tại của bạn. Để đặt biến
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 vĩnh viễn trên Windows, hãy tìm hiểu cách thêm vào PYTHONPATH trong Windows

Việc thêm các thư mục và tệp ZIP vào biến môi trường

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17 làm cho các mục nhập đó có sẵn cho bất kỳ trình thông dịch Python nào bạn chạy trong phiên cuối cùng. Cuối cùng, điều quan trọng cần lưu ý là Python sẽ âm thầm bỏ qua các thư mục và tệp ZIP không tồn tại được liệt kê trong
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
17, vì vậy hãy chú ý đến điều đó

Loại bỏ các quảng cáo

Sử dụng Tệp >>> import zipfile >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module: .. zip_module.writepy("hello.py") ... >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module: .. zip_module.printdir() ... File Name Modified Size hello.pyc 2021-10-18 05:40:04 313 18 để Nhập mã zip Toàn phiên dịch

Đôi khi, bạn có thể chỉ muốn nhập mã từ một tệp ZIP nhất định khi bạn đang chạy một trình thông dịch Python cụ thể. Điều này hữu ích khi bạn có một dự án sử dụng mã từ tệp ZIP đó và bạn không muốn mã có sẵn cho các dự án còn lại của mình

Các tệp cấu hình đường dẫn của Python cho phép bạn mở rộng

hello/
|
├── __init__.py
└── hello.py
7 của một trình thông dịch nhất định với các thư mục và tệp ZIP tùy chỉnh của bạn

Tệp cấu hình đường dẫn sử dụng phần mở rộng tệp

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18 và có thể chứa danh sách đường dẫn đến thư mục và tệp ZIP, mỗi đường dẫn trên một dòng. Danh sách các đường dẫn này được thêm vào
hello/
|
├── __init__.py
└── hello.py
7 mỗi khi bạn chạy trình thông dịch Python cung cấp tệp
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18

Các tệp

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18 của Python có định dạng đơn giản

  • Mỗi dòng phải chứa một mục nhập đường dẫn duy nhất
  • Các dòng trống và các dòng bắt đầu bằng ký hiệu số (_______1_______06) được bỏ qua
  • Các dòng bắt đầu bằng
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    8 được thực thi

Khi bạn có tệp

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18 phù hợp, bạn cần sao chép tệp đó vào một trong các thư mục của trang web để Python có thể tìm thấy và tải nội dung của tệp đó. Để có được các thư mục trang web của môi trường Python hiện tại của bạn, bạn có thể gọi từ mô-đun
hello/
|
├── __init__.py
└── hello.py
10. Nếu bạn không có đặc quyền quản trị viên trên máy hiện tại của mình thì bạn có thể sử dụng thư mục trang người dùng tại

Ghi chú. Thư mục trang web của người dùng có thể không tồn tại trong thư mục nhà của bạn. Nếu đó là trường hợp của bạn, thì hãy thoải mái tạo nó, theo cấu trúc đường dẫn bắt buộc

Ví dụ: lệnh sau tạo tệp cấu hình đường dẫn

hello/
|
├── __init__.py
└── hello.py
12 cho trình thông dịch Python 3 trên toàn hệ thống trên Ubuntu

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
1

Lệnh này tạo

hello/
|
├── __init__.py
└── hello.py
12, sử dụng trình soạn thảo văn bản nano GNU dưới dạng
hello/
|
├── __init__.py
└── hello.py
14. Khi đó, hãy nhập đường dẫn đến tệp
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 của bạn. Lưu tệp bằng cách nhấn Ctrl + X , sau đó Y, and finally Enter. Now this ZIP file will be available in
hello/
|
├── __init__.py
└── hello.py
7 when you launch the system Python interpreter again:

>>>

>>> import sys

>>> sys.path
[..., '/path/to/hello.zip', ...]

Đó là nó. Kể từ thời điểm này, bạn có thể nhập các đối tượng từ

>>> import sys

>>> # Insert the hello.zip into sys.path
>>> sys.path.insert(0, "/path/to/hello.zip")

>>> sys.path[0]
'/path/to/hello.zip'

>>> # Import and use the code
>>> import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
9 miễn là bạn sử dụng trình thông dịch Python trên toàn hệ thống

Một lần nữa, các thư mục và tệp ZIP không tồn tại sẽ không được thêm vào

hello/
|
├── __init__.py
└── hello.py
7 khi Python đọc và tải nội dung của một tệp
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18 đã cho. Cuối cùng, các mục lặp lại trong tệp
>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
18 chỉ được thêm một lần vào
hello/
|
├── __init__.py
└── hello.py
7

Khám phá hello/ | ├── __init__.py └── hello.py 0 của Python. Công cụ hỗ trợ nhập Zip

Bạn đã sử dụng mô-đun từ thư viện chuẩn mà không hề hay biết. Đằng sau hậu trường, cơ chế nhập tích hợp sẵn của Python tự động sử dụng mô-đun này khi một mục

hello/
|
├── __init__.py
└── hello.py
7 giữ đường dẫn đến tệp ZIP. Trong phần này, bạn sẽ tìm hiểu cách thức hoạt động của
hello/
|
├── __init__.py
└── hello.py
0 và cách sử dụng nó một cách rõ ràng trong mã của bạn với một ví dụ thực tế

Hiểu những điều cơ bản của hello/ | ├── __init__.py └── hello.py 0

Thành phần chính của

hello/
|
├── __init__.py
└── hello.py
0 là. Lớp này lấy đường dẫn đến tệp ZIP làm đối số và tạo một phiên bản trình nhập. Đây là một ví dụ về cách sử dụng
hello/
|
├── __init__.py
└── hello.py
28 và một số thuộc tính và phương thức của nó

>>>

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
3

Trong ví dụ này, trước tiên bạn nhập

hello/
|
├── __init__.py
└── hello.py
28 từ
hello/
|
├── __init__.py
└── hello.py
0. Sau đó, bạn tạo một phiên bản
hello/
|
├── __init__.py
└── hello.py
28 với đường dẫn đến tệp
>>> import sys

>>> sys.path.insert(0, "/path/to/hello_pkg.zip")

>>> from hello import hello

>>> hello.greet("Pythonista")
Hello, Pythonista! Welcome to Real Python!
2 của bạn

Lớp

hello/
|
├── __init__.py
└── hello.py
28 cung cấp một số thuộc tính và phương thức hữu ích. Ví dụ: trả về
hello/
|
├── __init__.py
└── hello.py
36 nếu tên đầu vào là gói và ngược lại là
hello/
|
├── __init__.py
└── hello.py
37. Phương thức trả về đường dẫn () đến một mô-đun nhất định bên trong kho lưu trữ

Nếu bạn muốn đưa tên của mô-đun vào không gian tên hiện tại của mình, thì bạn có thể sử dụng

hello/
|
├── __init__.py
└── hello.py
40, trả về một tham chiếu đến mô-đun đầu vào. Với tham chiếu đó, bạn có thể truy cập bất kỳ đối tượng mã nào từ mô-đun như bình thường

Loại bỏ các quảng cáo

Xây dựng hệ thống plugin với hello/ | ├── __init__.py └── hello.py 0

Như bạn đã học ở trên, Python sử dụng nội bộ

hello/
|
├── __init__.py
└── hello.py
0 để tải mã từ tệp ZIP. Bạn cũng biết rằng mô-đun này cung cấp các công cụ mà bạn có thể sử dụng trong một số tình huống viết mã thực tế. Ví dụ: giả sử bạn muốn triển khai hệ thống plugin tùy chỉnh trong đó mỗi plugin nằm trong tệp ZIP của riêng nó. Mã của bạn sẽ tìm kiếm các tệp ZIP trong một thư mục nhất định và tự động nhập chức năng của plugin

Để trải nghiệm ví dụ này trong thực tế, bạn sẽ triển khai hai plugin đồ chơi lấy thông báo và tiêu đề rồi hiển thị chúng trong cả trình duyệt web mặc định của bạn và hộp thông báo Tkinter. Mỗi plugin nên nằm trong thư mục riêng của nó, trong một mô-đun có tên là

hello/
|
├── __init__.py
└── hello.py
43. Mô-đun này sẽ triển khai chức năng của plugin và cung cấp chức năng
hello/
|
├── __init__.py
└── hello.py
44 làm điểm vào của plugin

Hãy tiếp tục và tạo một thư mục có tên

hello/
|
├── __init__.py
└── hello.py
45 với tệp
hello/
|
├── __init__.py
└── hello.py
43 trong đó. Mở tệp trong trình chỉnh sửa mã hoặc IDE yêu thích của bạn và nhập mã sau cho plugin trình duyệt web

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
4

Hàm

hello/
|
├── __init__.py
└── hello.py
44 trong mã này nhận thông báo
hello/
|
├── __init__.py
└── hello.py
48 và cửa sổ
hello/
|
├── __init__.py
└── hello.py
49. Sau đó, nó tạo ra một câu lệnh in a
hello/
|
├── __init__.py
└── hello.py
2. Tệp sẽ chứa một tài liệu HTML tối thiểu hiển thị
hello/
|
├── __init__.py
└── hello.py
49 và
hello/
|
├── __init__.py
└── hello.py
48 trên trang. Để mở tệp này trong trình duyệt web mặc định của bạn, bạn sử dụng
hello/
|
├── __init__.py
└── hello.py
54

Plugin tiếp theo cung cấp chức năng tương tự nhưng sử dụng bộ công cụ

hello/
|
├── __init__.py
└── hello.py
55. Mã cho plugin này cũng phải nằm trong một mô-đun có tên là
hello/
|
├── __init__.py
└── hello.py
43. Bạn có thể đặt mô-đun vào thư mục có tên là
hello/
|
├── __init__.py
└── hello.py
57 trong hệ thống tệp của mình

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
5

Theo mô hình tương tự như plugin trình duyệt web,

hello/
|
├── __init__.py
└── hello.py
44 lấy
hello/
|
├── __init__.py
└── hello.py
48 và
hello/
|
├── __init__.py
└── hello.py
49. Trong trường hợp này, chức năng tạo một phiên bản để giữ cửa sổ cấp cao nhất của plugin. Tuy nhiên bạn không cần hiện cửa sổ đó mà chỉ cần hiện hộp thông báo. Vì vậy, bạn sử dụng
hello/
|
├── __init__.py
└── hello.py
62 để ẩn các cửa sổ gốc và sau đó gọi
hello/
|
├── __init__.py
└── hello.py
63 trên
hello/
|
├── __init__.py
└── hello.py
64 để hiển thị hộp thoại với đầu vào
hello/
|
├── __init__.py
└── hello.py
48 và
hello/
|
├── __init__.py
└── hello.py
49

Bây giờ bạn cần đóng gói từng plugin vào tệp ZIP của riêng mình. Để làm như vậy, hãy bắt đầu phiên tương tác Python trong thư mục chứa các thư mục

hello/
|
├── __init__.py
└── hello.py
45 và
hello/
|
├── __init__.py
└── hello.py
57 và chạy đoạn mã sau

>>>

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
6

Bước tiếp theo là tạo thư mục gốc cho hệ thống plugin của bạn. Thư mục này phải chứa thư mục

hello/
|
├── __init__.py
└── hello.py
69 với các tệp ZIP mới được tạo trong đó. Đây là cách thư mục của bạn sẽ trông như thế nào

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
7

Trong

hello/
|
├── __init__.py
└── hello.py
70, bạn sẽ đặt mã máy khách cho hệ thống plugin của mình. Hãy tiếp tục và điền vào
hello/
|
├── __init__.py
└── hello.py
70 với mã sau

>>> import zipfile

>>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
..     zip_module.writepy("hello.py")
...

>>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
..     zip_module.printdir()
...
File Name                                             Modified             Size
hello.pyc                                      2021-10-18 05:40:04          313
8

Đây là cách mã này hoạt động theo từng dòng

  • Dòng 3 nhập
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    0 để tải động các plugin của bạn từ các tệp ZIP tương ứng
  • Dòng 4 nhập
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    73 để quản lý đường dẫn hệ thống
  • Dòng 6 định nghĩa
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    74, đưa đường dẫn đến thư mục chứa kho lưu trữ plugin của bạn
  • Dòng 7 tạo một danh sách trống để chứa các plugin hiện tại
  • Dòng 8 định nghĩa một vòng lặp
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    75 lặp qua các tệp
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    76 trong thư mục plugin
  • Dòng 9 tạo một phiên bản
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    28 cho mọi plugin trong hệ thống
  • Dòng 10 tải mô-đun
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    78 từ mỗi tệp ZIP của plugin
  • Dòng 11 nối chức năng
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    44 của mỗi plugin vào danh sách
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    80
  • Dòng 12 trả về danh sách
    hello/
    |
    ├── __init__.py
    └── hello.py
    
    80 cho người gọi

Các dòng 14 đến 18 gọi

hello/
|
├── __init__.py
└── hello.py
74 để tạo danh sách các plugin hiện có và thực hiện chúng trong một vòng lặp

Nếu bạn chạy tập lệnh

hello/
|
├── __init__.py
└── hello.py
70 từ dòng lệnh của mình, thì trước tiên bạn sẽ nhận được hộp thông báo Tkinter hiển thị thông báo
hello/
|
├── __init__.py
└── hello.py
84 và tiêu đề
hello/
|
├── __init__.py
└── hello.py
85. Sau khi đóng cửa sổ đó, trình duyệt web của bạn sẽ hiển thị cùng một thông báo và tiêu đề trên một trang mới. Đi trước và cung cấp cho nó một thử

Phần kết luận

Python có thể nhập mã trực tiếp từ tệp ZIP nếu chúng có sẵn trong đường dẫn tìm kiếm mô-đun. Tính năng này được gọi là nhập Zip. Bạn có thể tận dụng tính năng nhập Zip để gộp các mô-đun và gói vào một kho lưu trữ duy nhất để bạn có thể phân phối chúng cho người dùng cuối của mình một cách nhanh chóng và hiệu quả

Bạn cũng có thể tận dụng lợi thế của tính năng nhập Zip nếu bạn thường nhận được mã Python được gộp vào các tệp ZIP và cần sử dụng mã đó trong các tác vụ hàng ngày của mình

Trong hướng dẫn này, bạn đã học

  • Nhập Zip là gì
  • Thời điểm và cách sử dụng nhập Zip
  • Cách tạo các tệp ZIP có thể nhập bằng
    >>> import zipfile
    
    >>> with zipfile.PyZipFile("hello.zip", mode="w") as zip_module:
    ..     zip_module.writepy("hello.py")
    ...
    
    >>> with zipfile.PyZipFile("hello.zip", mode="r") as zip_module:
    ..     zip_module.printdir()
    ...
    File Name                                             Modified             Size
    hello.pyc                                      2021-10-18 05:40:04          313
    
    9
  • Cách cung cấp các tệp ZIP cho cơ chế nhập

Bạn cũng đã mã hóa một ví dụ thực hành về cách xây dựng một hệ thống plugin tối thiểu với

hello/
|
├── __init__.py
└── hello.py
0. Thông qua ví dụ này, bạn đã học cách nhập động mã từ tệp ZIP trong Python

Đánh dấu là đã hoàn thành

🐍 Thủ thuật Python 💌

Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python

Python nhập pyc từ đường dẫn

Gửi cho tôi thủ thuật Python »

Giới thiệu về Leodanis Pozo Ramos

Python nhập pyc từ đường dẫn
Python nhập pyc từ đường dẫn

Leodanis là một kỹ sư công nghiệp yêu thích Python và phát triển phần mềm. Anh ấy là một nhà phát triển Python tự học với hơn 6 năm kinh nghiệm. Anh ấy là một nhà văn đam mê kỹ thuật với số lượng bài báo được xuất bản ngày càng tăng trên Real Python và các trang web khác

» Tìm hiểu thêm về Leodanis


Mỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là

Python nhập pyc từ đường dẫn

Aldren

Python nhập pyc từ đường dẫn

Bartosz

Python nhập pyc từ đường dẫn

Geir Arne

Python nhập pyc từ đường dẫn

Ian

Python nhập pyc từ đường dẫn

kate

Python nhập pyc từ đường dẫn

Martin

Python nhập pyc từ đường dẫn

Sadie

Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Chuyên gia Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực

Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia

Nâng cao kỹ năng Python của bạn »

Bạn nghĩ sao?

Đánh giá bài viết này

Tweet Chia sẻ Chia sẻ Email

Bài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?

Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi

Làm cách nào để nhập mô-đun có đường dẫn trong Python?

Hàm append() . Đây là cách dễ nhất để nhập mô-đun Python bằng cách thêm đường dẫn mô-đun vào biến đường dẫn. Biến đường dẫn chứa các thư mục Trình thông dịch Python tìm kiếm các mô-đun đã được nhập trong tệp nguồn.

Tôi có thể nhập tệp PYC bằng Python không?

Tóm lại, để nhập tệp biên dịch Python (e. g. mô-đun. pyc), chỉ cần đặt nó trong cùng thư mục chứa nguồn (e. mô-đun g. py) và đảm bảo rằng không có tệp nguồn tương ứng (mô-đun. py trong ví dụ của chúng tôi) ở đó. Sau đó, mô-đun nhập thông thường sẽ hoạt động trơn tru

Làm cách nào để nhập mô-đun từ gói Python?

Nhập mô-đun từ một gói . ) using the dot (.) toán tử . Bây giờ, nếu mô-đun này chứa một hàm có tên select_difficulty() , chúng ta phải sử dụng tên đầy đủ để tham chiếu nó.