Làm cách nào để tạo gói python?

Như bạn thấy, các bước liên quan đến việc tạo các mô-đun và gói để nhập tương đối đơn giản. Tuy nhiên, việc thiết kế các mô-đun và gói hữu ích và dễ sử dụng cần rất nhiều kế hoạch và suy nghĩ.

Python nổi tiếng vì đi kèm với pin và nhiều khả năng phức tạp có sẵn trong thư viện tiêu chuẩn. Tuy nhiên, để khai thác hết tiềm năng của ngôn ngữ, bạn cũng nên tận dụng những đóng góp của cộng đồng tại PyPI. chỉ số đóng gói Python

PyPI, thường được phát âm là pie-pee-eye, là một kho chứa hàng trăm nghìn gói. Những phạm vi này từ triển khai

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
7 tầm thường đến thư viện học sâu nâng cao. Trong hướng dẫn này, bạn sẽ học cách tải gói của riêng mình lên PyPI. Xuất bản dự án của bạn dễ dàng hơn trước đây. Tuy nhiên, vẫn còn một vài bước liên quan

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

  • Chuẩn bị gói Python của bạn để xuất bản
  • Xử lý phiên bản gói của bạn
  • Xây dựng gói của bạn và tải nó lên PyPI
  • Hiểu và sử dụng các hệ thống xây dựng khác nhau

Trong suốt hướng dẫn này, bạn sẽ làm việc với một dự án ví dụ. gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 có thể được sử dụng để đọc các hướng dẫn Real Python trong bảng điều khiển của bạn. Bạn sẽ được giới thiệu nhanh về dự án trước khi đi sâu vào cách xuất bản gói này. Nhấp vào liên kết bên dưới để truy cập kho lưu trữ GitHub chứa mã nguồn đầy đủ của
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8

Nhận mã nguồn. Nhấp vào đây để có quyền truy cập vào mã nguồn của Real Python Feed Reader mà bạn sẽ làm việc trong hướng dẫn này

Tìm hiểu Bao bì Python

Đóng gói trong Python có vẻ phức tạp và khó hiểu đối với cả người mới và người có kinh nghiệm lâu năm. Bạn sẽ tìm thấy những lời khuyên trái ngược nhau trên Internet và những gì từng được coi là thông lệ tốt giờ đây có thể bị phản đối

Lý do chính của tình trạng này là Python là một ngôn ngữ lập trình khá cũ. Thật vậy, phiên bản đầu tiên của Python được phát hành vào năm 1991, trước khi World Wide Web ra mắt công chúng. Đương nhiên, một hệ thống dựa trên web, hiện đại để phân phối các gói không được bao gồm hoặc thậm chí không được lên kế hoạch cho các phiên bản đầu tiên của Python

Thay vào đó, hệ sinh thái đóng gói của Python đã phát triển hữu cơ qua nhiều thập kỷ khi nhu cầu của người dùng trở nên rõ ràng và công nghệ mang đến những khả năng mới. Hỗ trợ đóng gói đầu tiên xuất hiện vào mùa thu năm 2000, với thư viện

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
0 được đưa vào Python 1. 6 và 2. 0. Chỉ số đóng gói Python [PyPI] xuất hiện trực tuyến vào năm 2003, ban đầu là một chỉ mục thuần túy của các gói hiện có, không có bất kỳ khả năng lưu trữ nào

Ghi chú. PyPI thường được gọi là Cửa hàng pho mát Python liên quan đến bản phác thảo Cửa hàng pho mát nổi tiếng của Monty Python. Cho đến ngày nay,

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
1 chuyển hướng đến PyPI

Trong thập kỷ qua, nhiều sáng kiến ​​đã cải thiện bối cảnh đóng gói, đưa nó từ miền Tây hoang dã trở thành một hệ thống khá hiện đại và hiệu quả. Điều này chủ yếu được thực hiện thông qua các Đề xuất cải tiến Python [PEP] được xem xét và triển khai bởi nhóm làm việc của Cơ quan quản lý bao bì Python [PyPA]

Các tài liệu quan trọng nhất xác định cách hoạt động của gói Python là các PEP sau

  • PEP 427 mô tả cách đóng gói bánh xe
  • PEP 440 mô tả cách phân tích cú pháp số phiên bản
  • PEP 508 mô tả cách chỉ định các phụ thuộc
  • PEP 517 mô tả cách hoạt động của phần phụ trợ xây dựng
  • PEP 518 mô tả cách chỉ định một hệ thống xây dựng
  • PEP 621 mô tả cách viết siêu dữ liệu dự án
  • PEP 660 mô tả cách thực hiện cài đặt có thể chỉnh sửa

Bạn không cần nghiên cứu các tài liệu kỹ thuật này. Trong hướng dẫn này, bạn sẽ tìm hiểu cách tất cả các thông số kỹ thuật này kết hợp với nhau trong thực tế khi bạn thực hiện quy trình xuất bản gói của riêng mình

Để có cái nhìn tổng quan về lịch sử đóng gói Python, hãy xem bài thuyết trình của Thomas Kluyver tại PyCon UK 2019. bao bì trăn. Làm thế nào chúng ta đến được đây, và chúng ta sẽ đi đâu?

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

Tạo một gói Python nhỏ

Trong phần này, bạn sẽ biết một gói Python nhỏ mà bạn có thể sử dụng làm ví dụ để có thể xuất bản lên PyPI. Nếu bạn đã có gói của riêng mình mà bạn muốn xuất bản, thì vui lòng đọc lướt qua phần này và tham gia lại ở phần tiếp theo

Gói mà bạn sẽ thấy ở đây có tên là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Nó có thể được sử dụng như một thư viện để tải xuống các hướng dẫn Real Python bằng mã của riêng bạn và như một ứng dụng để đọc các hướng dẫn trong bảng điều khiển của bạn

Ghi chú. Mã nguồn như được hiển thị và giải thích trong phần này là phiên bản đơn giản hóa—nhưng đầy đủ chức năng—của trình đọc nguồn cấp dữ liệu Python thực. So với phiên bản hiện được xuất bản trên PyPI, phiên bản này thiếu một số tùy chọn xử lý lỗi và bổ sung

Đầu tiên, hãy xem cấu trúc thư mục của

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Gói nằm hoàn toàn bên trong một thư mục có thể được đặt tên bất cứ thứ gì. Trong trường hợp này, nó được đặt tên là
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
4. Mã nguồn được bao bọc bên trong một thư mục
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
5. Điều này không thực sự cần thiết, nhưng nó thường là một ý kiến ​​hay

Ghi chú. Việc sử dụng thêm một thư mục

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
5 khi cấu trúc các gói đã là một điểm thảo luận trong cộng đồng Python trong nhiều năm. Nói chung, cấu trúc thư mục phẳng dễ bắt đầu hơn một chút, nhưng cấu trúc
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
5 cung cấp một số lợi thế khi dự án của bạn phát triển

Thư mục

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
8 bên trong chứa tất cả mã nguồn của bạn

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
2

Mã nguồn của gói nằm trong thư mục con

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
5 cùng với tệp cấu hình. Có một vài bài kiểm tra trong thư mục con
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
40 riêng biệt. Bản thân các bài kiểm tra sẽ không được đề cập trong hướng dẫn này, nhưng bạn sẽ học cách xử lý các thư mục kiểm tra sau. Bạn có thể tìm hiểu thêm về kiểm thử nói chung trong Bắt đầu với kiểm thử bằng Python và Kiểm thử Python hiệu quả với Pytest

Nếu bạn đang làm việc với gói của riêng mình thì bạn có thể sử dụng cấu trúc khác hoặc có các tệp khác trong thư mục gói của mình. Bố cục ứng dụng Python thảo luận về một số tùy chọn khác nhau. Các bước bên dưới để xuất bản lên PyPI sẽ hoạt động độc lập với bố cục bạn sử dụng

Trong phần còn lại của phần này, bạn sẽ thấy cách hoạt động của gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Trong phần tiếp theo, bạn sẽ tìm hiểu thêm về các tệp đặc biệt như
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
42,
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
43,
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
44 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 mà bạn sẽ cần để xuất bản gói của mình

Sử dụng Trình đọc Python thực

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 là trình đọc nguồn cấp dữ liệu web cơ bản có thể tải xuống các hướng dẫn Real Python mới nhất từ ​​nguồn cấp dữ liệu Python thực

Trong phần này, trước tiên bạn sẽ thấy một vài ví dụ về đầu ra mà bạn có thể mong đợi từ

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Bạn chưa thể tự chạy các ví dụ này, nhưng chúng sẽ cung cấp cho bạn một số ý tưởng về cách thức hoạt động của công cụ

Ghi chú. Nếu bạn đã tải xuống mã nguồn của

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8, thì bạn có thể làm theo bằng cách tạo môi trường ảo trước rồi cài đặt gói cục bộ bên trong môi trường ảo đó

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
3

Trong suốt hướng dẫn, bạn sẽ tìm hiểu thêm về những gì xảy ra khi bạn chạy lệnh này

Ví dụ đầu tiên sử dụng trình đọc để lấy danh sách các bài viết mới nhất

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
4

Danh sách này hiển thị các hướng dẫn gần đây nhất, vì vậy danh sách của bạn có thể khác với những gì bạn thấy ở trên. Tuy nhiên, lưu ý rằng mỗi bài viết được đánh số. Để đọc một hướng dẫn cụ thể, bạn sử dụng cùng một lệnh nhưng bao gồm cả số của hướng dẫn đó

Ghi chú. Nguồn cấp dữ liệu Python thực chứa các bản xem trước hạn chế của các bài viết. Do đó, bạn sẽ không thể đọc toàn bộ hướng dẫn với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8

Trong trường hợp này, để đọc Cách xuất bản Gói Python mã nguồn mở lên PyPI, bạn thêm

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
60 vào lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
7

Điều này in bài viết ra bàn điều khiển bằng định dạng Markdown

Ghi chú.

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
61 được sử dụng để thực thi một mô-đun hoặc một gói. Nó hoạt động tương tự như
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
62 cho các mô-đun và tập lệnh thông thường. Ví dụ:
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
63 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
64 hầu hết là tương đương

Khi bạn chạy một gói có

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
65, tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66 trong gói sẽ được thực thi. Xem Gọi cho Reader để biết thêm thông tin

Hiện tại, bạn sẽ cần chạy lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
67 từ bên trong thư mục
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
5. Sau đó, bạn sẽ học cách chạy lệnh từ bất kỳ thư mục làm việc nào

Bằng cách thay đổi số trên dòng lệnh, bạn có thể đọc bất kỳ hướng dẫn nào có sẵn

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

Hiểu mã người đọc

Các chi tiết về cách hoạt động của

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 không quan trọng đối với mục đích của hướng dẫn này. Tuy nhiên, nếu bạn muốn tìm hiểu thêm về cách triển khai, thì bạn có thể mở rộng các phần bên dưới. Gói bao gồm năm tệp

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
70Hiển thị/Ẩn

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
70 là tệp cấu hình được sử dụng để chỉ định URL của nguồn cấp dữ liệu của các hướng dẫn Real Python. Đó là một tệp văn bản mà thư viện bên thứ ba của
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
72 có thể đọc được

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
0

Nói chung, các tệp TOML chứa các cặp khóa-giá trị được phân tách thành các phần hoặc bảng. Tệp cụ thể này chỉ chứa một phần,

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
73, với một khóa,
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
74

Ghi chú. Tệp cấu hình có thể quá mức cần thiết cho gói đơn giản này. Thay vào đó, bạn có thể xác định URL dưới dạng hằng số cấp mô-đun trực tiếp trong mã nguồn của mình. Tệp cấu hình được bao gồm ở đây để minh họa cách làm việc với các tệp không phải mã

TOML là một định dạng tệp cấu hình đã trở nên phổ biến gần đây. Python sử dụng nó cho tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 mà bạn sẽ tìm hiểu sau. Để tìm hiểu sâu hơn về TOML, hãy xem Python và TOML. Những người bạn tốt nhất mới

Hỗ trợ đọc tệp TOML sẽ được thêm vào thư viện chuẩn trong Python 3. 11 với thư viện

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
76 mới. Cho đến lúc đó, bạn có thể sử dụng gói
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
72 của bên thứ ba

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66Hiển thị/Ẩn

Tệp mã nguồn đầu tiên mà bạn sẽ xem là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66. Dấu gạch dưới kép cho biết tệp này có ý nghĩa đặc biệt trong Python. Thật vậy, khi thực thi một gói có
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
61 như bạn đã làm trước đó, Python sẽ chạy nội dung của
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66

Nói cách khác,

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66 đóng vai trò là điểm vào chương trình của bạn và đảm nhiệm luồng chính, gọi các phần khác khi cần

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]

Lưu ý rằng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
303 được gọi ở dòng cuối cùng. Nếu bạn không gọi
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
303, thì chương trình sẽ không làm gì cả. Như bạn đã thấy trước đó, chương trình có thể liệt kê tất cả các hướng dẫn hoặc in một hướng dẫn cụ thể. Điều này được xử lý bởi khối ________ 2305 … ________ 2306 trên các dòng 11 đến 19

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
307Hiển thị/Ẩn

Tệp tiếp theo là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
307. Một lần nữa, hai dấu gạch dưới trong tên tệp cho bạn biết rằng đây là một tệp đặc biệt.
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
307 đại diện cho thư mục gốc của gói của bạn. Nó thường được giữ khá đơn giản, nhưng đó là một nơi tốt để đặt các hằng gói, tài liệu, v.v.

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]

Biến đặc biệt

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
310 là một quy ước trong Python để thêm số phiên bản vào gói của bạn. Nó được giới thiệu trong PEP 396. Bạn sẽ tìm hiểu thêm về lập phiên bản sau

Ghi chú. Bạn có thể sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
311 để kiểm tra phiên bản của bất kỳ gói nào mà bạn đã cài đặt

>>>

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
4

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
311 xác định các gói theo tên PyPI của chúng, đó là lý do tại sao bạn tra cứu
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
313 chứ không phải
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Bạn sẽ tìm hiểu thêm về các tên khác nhau cho dự án của mình sau

Chỉ định

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
310 là—nói đúng ra—không cần thiết. Máy móc
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
311 sử dụng siêu dữ liệu dự án để tìm số phiên bản. Tuy nhiên, đây vẫn là một quy ước hữu ích cần tuân theo và các công cụ như Setuptools và Flit có thể sử dụng quy ước này để tự động cập nhật siêu dữ liệu gói của bạn

Để đọc URL tới nguồn cấp dữ liệu từ tệp cấu hình, bạn sử dụng

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
76 hoặc
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
72 để được hỗ trợ TOML. Sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
319 …
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
320 như trên đảm bảo rằng bạn sử dụng
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
76 nếu có sẵn và bạn quay lại
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
72 nếu không có

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
323 được sử dụng để nhập các tệp không phải mã hoặc tài nguyên từ một gói mà không cần phải tìm ra đường dẫn tệp đầy đủ của chúng. Điều này đặc biệt hữu ích khi bạn xuất bản gói của mình lên PyPI và không có toàn quyền kiểm soát nơi gói của bạn được cài đặt và cách sử dụng gói đó. Các tệp tài nguyên thậm chí có thể kết thúc bên trong kho lưu trữ zip

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
323 đã trở thành một phần của thư viện chuẩn của Python trong Python 3. 7. Xem bài thuyết trình của Barry Warsaw tại PyCon 2018 để biết thêm thông tin

Các biến được xác định trong

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
307 trở nên khả dụng dưới dạng các biến trong không gian tên gói

>>>

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
6

Bạn có thể truy cập các hằng số gói dưới dạng thuộc tính trực tiếp trên

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
327Hiển thị/Ẩn

Trong

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66, bạn nhập hai mô-đun,
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
73 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
330 và sử dụng các mô-đun đó để đọc từ nguồn cấp dữ liệu và hiển thị kết quả. Những mô-đun đó thực hiện hầu hết các công việc thực tế

Trước tiên hãy xem xét

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
327. Tệp này chứa các hàm để đọc từ nguồn cấp dữ liệu web và phân tích kết quả. May mắn thay, đã có sẵn những thư viện tuyệt vời để làm điều này.
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
327 phụ thuộc vào hai mô-đun đã có sẵn trên PyPI.
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
333 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
334

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
327 bao gồm một số chức năng. Bạn sẽ nhìn vào chúng cùng một lúc

Đọc một nguồn cấp dữ liệu web có thể mất một chút thời gian. Để tránh đọc nguồn cấp dữ liệu web nhiều hơn mức cần thiết, bạn sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
336 để lưu nguồn cấp dữ liệu vào bộ nhớ đệm trong lần đọc đầu tiên

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
7

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
337 đọc một nguồn cấp dữ liệu từ web và trả về nó trong một cấu trúc trông giống như một cuốn từ điển. Để tránh tải xuống nguồn cấp dữ liệu nhiều lần, chức năng này được trang trí bằng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
336, ghi nhớ giá trị được trả về bởi
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
339 và sử dụng lại giá trị đó trong các lần gọi sau

Ghi chú. Trình trang trí

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
336 đã được giới thiệu cho
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
341 trong Python 3. 9. Trên các phiên bản Python cũ hơn, bạn có thể sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
342 để thay thế

Bạn thêm tiền tố

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
339 với dấu gạch dưới để cho biết rằng đó là một chức năng hỗ trợ và không dành cho người dùng gói của bạn sử dụng trực tiếp

Bạn có thể nhận được một số thông tin cơ bản về nguồn cấp dữ liệu bằng cách xem trong siêu dữ liệu

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
344. Hàm sau chọn ra tiêu đề và liên kết đến trang web chứa nguồn cấp dữ liệu

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
30

Ngoài

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
345 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
346, các thuộc tính như
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
347,
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
348 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
349 cũng có sẵn

Các bài viết có sẵn trong nguồn cấp dữ liệu có thể được tìm thấy trong danh sách

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
350. Tiêu đề bài viết có thể được tìm thấy với một danh sách hiểu

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
31

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
350 liệt kê các bài viết trong nguồn cấp dữ liệu được sắp xếp theo thứ tự thời gian, sao cho bài viết mới nhất là
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
352

Để có được nội dung của một bài viết cụ thể, bạn sử dụng chỉ mục của nó trong

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
350 làm ID bài viết

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
32

Sau khi chọn đúng bài báo từ

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
350, bạn tìm văn bản của bài viết ở định dạng HTML và lưu trữ dưới dạng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
355. Tiếp theo,
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
334 thực hiện tốt công việc dịch HTML thành văn bản Markdown dễ đọc hơn. HTML không chứa tiêu đề bài viết, vì vậy tiêu đề được thêm vào trước khi nội dung bài viết được trả về

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
357Hiển thị/Ẩn

Mô-đun cuối cùng là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
357. Nó bao gồm hai chức năng nhỏ. Trong thực tế, bạn có thể sử dụng trực tiếp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
359 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66 thay vì gọi hàm
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
330. Tuy nhiên, việc tách chức năng ra giúp việc thay thế nó bằng thứ gì đó cao cấp hơn sau này trở nên đơn giản hơn

Tại đây, bạn sử dụng các câu lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
359 đơn giản để hiển thị nội dung cho người dùng. Để cải tiến, có thể bạn muốn thêm định dạng phong phú hơn hoặc giao diện GUI cho trình đọc của mình. Để làm như vậy, bạn chỉ cần thay thế hai chức năng sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
33

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
363 in một bài báo lên bảng điều khiển, trong khi
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
364 in danh sách tiêu đề. Cái sau cũng tạo ID bài viết được sử dụng khi chọn đọc một bài viết cụ thể

Ngoài các tệp mã nguồn này, bạn cần thêm một số tệp đặc biệt trước khi có thể xuất bản gói của mình. Bạn sẽ đề cập đến các tệp đó trong các phần sau

Gọi người đọc

Một thách thức khi dự án của bạn phát triển phức tạp là cho phép người dùng của bạn biết cách họ có thể sử dụng dự án của bạn. Vì

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 bao gồm bốn tệp mã nguồn khác nhau, làm cách nào để người dùng biết tệp nào cần thực thi để sử dụng ứng dụng?

Ghi chú. Một tệp Python đơn lẻ thường được gọi là tập lệnh hoặc mô-đun. Bạn có thể nghĩ về một gói như một tập hợp các mô-đun

Thông thường nhất, bạn chạy tập lệnh Python bằng cách cung cấp tên tệp của nó. Chẳng hạn, nếu bạn có tập lệnh có tên là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
366, thì bạn có thể chạy tập lệnh đó như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
34

Tập lệnh giả định này in

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
367 ra bảng điều khiển của bạn khi bạn chạy nó. Tương tự, bạn có thể sử dụng tùy chọn
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
65 của chương trình thông dịch
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
62 để chạy tập lệnh bằng cách chỉ định tên mô-đun thay vì tên tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
35

Đối với các mô-đun trong thư mục hiện tại của bạn, tên mô-đun giống với tên tệp ngoại trừ hậu tố

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
370 bị bỏ đi

Một lợi thế của việc sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
65 là nó cho phép bạn gọi tất cả các mô-đun trong đường dẫn Python của bạn, bao gồm cả những mô-đun được tích hợp trong Python. Một ví dụ đang gọi
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
372

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
36

Nếu bạn muốn chạy một mô-đun tích hợp mà không có

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
65, thì trước tiên bạn cần tra cứu nơi lưu trữ mô-đun đó trên hệ thống của mình, sau đó gọi mô-đun đó với đường dẫn đầy đủ của nó

Một ưu điểm khác của việc sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
65 là nó hoạt động cho các gói cũng như các mô-đun. Như bạn đã học trước đó, bạn có thể gọi gói
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 với
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
65 miễn là thư mục
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
377 có sẵn trong thư mục làm việc của bạn

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
37

Bởi vì

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 là một gói, tên chỉ đề cập đến một thư mục. Python quyết định mã nào bên trong thư mục đó sẽ chạy như thế nào? . Nếu một tệp như vậy tồn tại, thì nó sẽ được thực thi. Nếu nó không tồn tại, thì một thông báo lỗi sẽ được in

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
38

Thông báo lỗi nói rằng gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
380 của thư viện tiêu chuẩn chưa xác định tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66

Nếu bạn đang tạo một gói được cho là sẽ được thực thi, thì bạn nên bao gồm tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
66. Bạn cũng có thể làm theo ví dụ tuyệt vời của Rich về việc sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
383 để chứng minh khả năng của gói của bạn

Sau này, bạn sẽ thấy cách bạn cũng có thể tạo các điểm vào gói của mình hoạt động giống như các chương trình dòng lệnh thông thường. Những thứ này thậm chí sẽ dễ dàng hơn cho người dùng cuối của bạn sử dụng

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

Chuẩn bị gói của bạn để xuất bản

Bạn đã có một gói mà bạn muốn xuất bản. Có thể bạn đã sao chép

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 hoặc có thể bạn có gói của riêng mình. Trong phần này, bạn sẽ thấy những bước bạn cần thực hiện trước khi tải gói của mình lên PyPI

Đặt tên cho gói của bạn

Bước đầu tiên—và có thể là khó nhất—là nghĩ ra một cái tên hay cho gói hàng của bạn. Tất cả các gói trên PyPI cần phải có tên duy nhất. Hiện có vài trăm nghìn gói trên PyPI, vì vậy rất có thể tên yêu thích của bạn đã được sử dụng

Ví dụ, đã có một gói trên PyPI có tên là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Một cách để làm cho tên gói trở nên độc đáo là thêm tiền tố dễ nhận biết vào tên. Trong ví dụ này, bạn sẽ sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
313 làm tên PyPI cho gói
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8

Dù bạn chọn tên PyPI nào cho gói của mình, đó sẽ là tên bạn sẽ sử dụng khi cài đặt với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
39

Lưu ý rằng tên PyPI không cần khớp với tên gói. Ở đây, gói vẫn được đặt tên là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 và đó là tên bạn cần sử dụng khi nhập gói

>>>

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
40

Đôi khi bạn cần sử dụng các tên khác nhau cho gói của mình. Tuy nhiên, bạn đang giữ mọi thứ đơn giản hơn cho người dùng của mình nếu tên gói và tên PyPI giống nhau

Xin lưu ý rằng mặc dù tên gói không cần phải là duy nhất trên toàn cầu như tên PyPI, nhưng nó cần phải là duy nhất trên môi trường mà bạn đang chạy nó trong đó

Nếu bạn cài đặt hai gói có cùng tên gói, ví dụ như

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
313, thì câu lệnh như
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
392 là không rõ ràng. Python giải quyết vấn đề này bằng cách nhập gói mà nó tìm thấy đầu tiên trong đường dẫn nhập. Thông thường, đây sẽ là gói đầu tiên khi sắp xếp tên theo thứ tự bảng chữ cái. Tuy nhiên, bạn không nên phụ thuộc vào hành vi này

Thông thường, bạn muốn tên gói của mình càng độc đáo càng tốt, đồng thời cân bằng điều này với sự tiện lợi của một tên ngắn gọn và súc tích.

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
313 là một trình đọc nguồn cấp dữ liệu chuyên dụng, trong khi
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 trên PyPI là tổng quát hơn. Đối với mục đích của hướng dẫn này, không có lý do gì để cần cả hai, vì vậy thỏa hiệp với tên không phải duy nhất có thể đáng giá

Định cấu hình gói của bạn

Để chuẩn bị gói của bạn xuất bản trên PyPI, bạn cần cung cấp một số thông tin về nó. Nói chung, bạn cần chỉ định hai loại thông tin

  1. Cấu hình hệ thống xây dựng của bạn
  2. Cấu hình gói của bạn

Hệ thống xây dựng chịu trách nhiệm tạo các tệp thực tế mà bạn sẽ tải lên PyPI, thường ở định dạng bánh xe hoặc phân phối nguồn [sdist]. Trong một thời gian dài, việc này được thực hiện bởi

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
0 hoặc
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
396. Tuy nhiên, PEP 517 và PEP 518 đã giới thiệu một cách để chỉ định các hệ thống xây dựng tùy chỉnh

Ghi chú. Bạn có thể chọn hệ thống xây dựng nào bạn sử dụng trong các dự án của mình. Sự khác biệt chính giữa các hệ thống xây dựng khác nhau là cách bạn định cấu hình gói của mình và lệnh nào bạn chạy để xây dựng và tải gói của mình lên

Hướng dẫn này sẽ tập trung vào việc sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
396 làm hệ thống xây dựng. Tuy nhiên, sau này bạn sẽ học cách sử dụng các lựa chọn thay thế như Flit và Thơ

Mỗi dự án Python nên sử dụng một tệp có tên

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 để chỉ định hệ thống xây dựng của nó. Bạn có thể sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
396 bằng cách thêm phần sau vào
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
41

Điều này xác định rằng bạn đang sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
396 làm hệ thống xây dựng cũng như phần phụ thuộc mà Python phải cài đặt để xây dựng gói của bạn. Thông thường, tài liệu của hệ thống xây dựng bạn chọn sẽ cho bạn biết cách viết bảng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
402 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45

Thông tin thú vị hơn mà bạn cần cung cấp liên quan đến chính gói hàng của bạn. PEP 621 xác định cách siêu dữ liệu về gói của bạn cũng có thể được đưa vào

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 theo cách thống nhất nhất có thể trên các hệ thống xây dựng khác nhau

Ghi chú. Trước đây, Setuptools đã sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
405 để định cấu hình gói của bạn. Bởi vì đây là tập lệnh Python thực tế chạy khi cài đặt nên nó rất mạnh và vẫn có thể được yêu cầu khi xây dựng các gói phức tạp

Tuy nhiên, tốt hơn hết là bạn nên sử dụng tệp cấu hình khai báo để trình bày cách xây dựng gói của mình, vì nó dễ giải thích hơn và có ít cạm bẫy hơn để lo lắng. Sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
406 là cách phổ biến nhất để định cấu hình Setuptools

Tuy nhiên, Setuptools đang chuyển sang sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 như được chỉ định trong PEP 621. Trong hướng dẫn này, bạn sẽ sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 cho tất cả cấu hình gói của mình

Một cấu hình khá tối thiểu của gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 có thể trông như thế này

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
42

Hầu hết thông tin này là tùy chọn và có các cài đặt khác mà bạn có thể sử dụng không có trong ví dụ này. Kiểm tra các tài liệu cho tất cả các chi tiết

Thông tin tối thiểu mà bạn phải đưa vào

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 của mình là thông tin sau

  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    411 chỉ định tên gói của bạn vì nó sẽ xuất hiện trên PyPI
  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    412 đặt phiên bản hiện tại cho gói của bạn

Như ví dụ trên cho thấy, bạn có thể bao gồm nhiều thông tin hơn. Một số khóa khác trong

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 được diễn giải như sau

  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    414 mô tả dự án của bạn bằng cách sử dụng danh sách các bộ phân loại. Bạn nên sử dụng chúng vì chúng làm cho dự án của bạn dễ tìm kiếm hơn
  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    415 liệt kê bất kỳ sự phụ thuộc nào mà gói của bạn có đối với các thư viện của bên thứ ba.
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    8 phụ thuộc vào
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    333,
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    334 và
    # __init__.py
    
    from importlib import resources
    try:
        import tomllib
    except ModuleNotFoundError:
        import tomli as tomllib
    
    # Version of the realpython-reader package
    __version__ = "1.0.0"
    
    # Read URL of the Real Python feed from config file
    _cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
    URL = _cfg["feed"]["url"]
    
    72, vì vậy chúng được liệt kê ở đây
  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    420 thêm các liên kết mà bạn có thể sử dụng để trình bày thông tin bổ sung về gói hàng của mình cho người dùng. Bạn có thể bao gồm một số liên kết ở đây
  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    421 tạo các tập lệnh dòng lệnh gọi các hàm trong gói của bạn. Ở đây, lệnh
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    422 mới gọi
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    303 trong mô-đun
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    424

Bảng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
421 là một trong ba bảng có thể xử lý các điểm vào. Bạn cũng có thể bao gồm
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
426 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
427, tương ứng chỉ định các ứng dụng GUI và plugin

Mục đích của tất cả thông tin này là làm cho gói của bạn hấp dẫn và dễ tìm thấy trên PyPI. Hãy xem trang dự án

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
313 trên PyPI và so sánh thông tin với
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 ở trên

Tất cả thông tin về PyPI đến từ

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
44. Ví dụ: số phiên bản dựa trên dòng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
432 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
433, trong khi Đọc hướng dẫn Real Python mới nhất được sao chép từ
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
434

Hơn nữa, mô tả dự án được lấy từ tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
44 của bạn. Trong thanh bên, bạn có thể tìm thấy thông tin từ
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
420 trong phần Liên kết dự án và từ
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
437 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
438 trong phần Meta. Các giá trị bạn đã chỉ định trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
414 hiển thị ở cuối thanh bên

Xem PEP 621 để biết chi tiết về tất cả các phím. Bạn sẽ tìm hiểu thêm về

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
415, cũng như
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
441, trong tiểu mục tiếp theo

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

Chỉ định gói phụ thuộc của bạn

Gói của bạn có thể sẽ phụ thuộc vào các thư viện của bên thứ ba không phải là một phần của thư viện tiêu chuẩn. Bạn nên chỉ định những điều này trong danh sách

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
415 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45. Trong ví dụ trên, bạn đã làm như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
43

Điều này xác định rằng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 phụ thuộc vào
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
333,
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
334 và
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
72. Hơn nữa, nó nói như sau

  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    333 phải là phiên bản 5. 2. 0 trở lên
  •  1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    334 có thể là bất kỳ phiên bản nào
  • # __init__.py
    
    from importlib import resources
    try:
        import tomllib
    except ModuleNotFoundError:
        import tomli as tomllib
    
    # Version of the realpython-reader package
    __version__ = "1.0.0"
    
    # Read URL of the Real Python feed from config file
    _cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
    URL = _cfg["feed"]["url"]
    
    72 có thể là bất kỳ phiên bản nào, nhưng chỉ bắt buộc trên Python 3. 10 hoặc sớm hơn

Điều này cho thấy một số khả năng mà bạn có thể sử dụng khi chỉ định các thành phần phụ thuộc, bao gồm các chỉ định phiên bản và đánh dấu môi trường. Bạn có thể sử dụng cái sau để giải thích cho các hệ điều hành, phiên bản Python khác nhau, v.v.

Tuy nhiên, lưu ý rằng bạn nên cố gắng chỉ xác định các yêu cầu tối thiểu cần thiết để thư viện hoặc ứng dụng của bạn hoạt động. Danh sách này sẽ được sử dụng bởi

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 để giải quyết các phụ thuộc bất cứ khi nào gói của bạn được cài đặt. Bằng cách giữ danh sách này ở mức tối thiểu, bạn đảm bảo rằng gói của mình tương thích nhất có thể

Bạn có thể đã nghe nói rằng bạn nên ghim các phần phụ thuộc của mình. Đó là lời khuyên tuyệt vời. Tuy nhiên, nó không giữ trong trường hợp này. Bạn ghim các phụ thuộc của mình để đảm bảo môi trường của bạn có thể tái sản xuất. Mặt khác, gói của bạn hy vọng sẽ hoạt động trên nhiều môi trường Python khác nhau

Khi thêm các gói vào

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
415, bạn nên tuân theo các quy tắc chung này

  • Chỉ liệt kê các phụ thuộc trực tiếp của bạn. Ví dụ:
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    8 nhập khẩu
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    333,
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    334 và
    # __init__.py
    
    from importlib import resources
    try:
        import tomllib
    except ModuleNotFoundError:
        import tomli as tomllib
    
    # Version of the realpython-reader package
    __version__ = "1.0.0"
    
    # Read URL of the Real Python feed from config file
    _cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
    URL = _cfg["feed"]["url"]
    
    72, vì vậy chúng được liệt kê. Mặt khác,
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    333 phụ thuộc vào
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    458, nhưng
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    8 không trực tiếp sử dụng thư viện này, vì vậy nó không được chỉ định
  • Không bao giờ ghim phụ thuộc của bạn vào một phiên bản cụ thể với
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    460
  • Sử dụng
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    461 để thêm giới hạn dưới nếu bạn phụ thuộc vào chức năng đã được thêm vào trong một phiên bản phụ thuộc cụ thể của bạn
  • Sử dụng
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    462 để thêm giới hạn trên nếu bạn lo lắng rằng phần phụ thuộc có thể phá vỡ khả năng tương thích trong bản nâng cấp phiên bản chính. Trong trường hợp này, bạn nên siêng năng kiểm tra các bản nâng cấp đó và xóa hoặc tăng giới hạn trên nếu có thể

Lưu ý rằng các quy tắc này được áp dụng khi bạn định cấu hình gói mà bạn đang cung cấp cho người khác. Nếu bạn đang triển khai gói của mình, thì bạn nên ghim các phần phụ thuộc của mình bên trong môi trường ảo

Dự án công cụ pip là một cách tuyệt vời để quản lý các phụ thuộc được ghim. Nó đi kèm với lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
463 có thể tạo hoặc cập nhật danh sách đầy đủ các phụ thuộc

Ví dụ: giả sử bạn đang triển khai

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 vào một môi trường ảo. Sau đó, bạn có thể tạo một môi trường có thể tái tạo bằng pip-tools. Trên thực tế,
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
463 có thể hoạt động trực tiếp với tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 của bạn

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
44

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
463 tạo một tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
468 chi tiết với nội dung tương tự như đầu ra ở trên. Bạn có thể sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
469 hoặc
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
470 để cài đặt các phụ thuộc này vào môi trường của mình

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45

Xem tài liệu về công cụ pip để biết thêm thông tin

Bạn cũng được phép chỉ định các thành phần phụ thuộc tùy chọn của gói trong một bảng riêng có tên là

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
441. Bạn thường sử dụng điều này để chỉ định các phụ thuộc mà bạn sử dụng trong quá trình phát triển hoặc thử nghiệm. Tuy nhiên, bạn cũng có thể chỉ định các phụ thuộc bổ sung được sử dụng để hỗ trợ các tính năng nhất định trong gói của mình

Trong ví dụ trên, bạn đã bao gồm phần sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
46

Điều này thêm một nhóm,

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
472, phụ thuộc tùy chọn. Bạn có thể có một số nhóm như vậy và bạn có thể đặt tên cho các nhóm theo cách hợp lý

Theo mặc định, các phụ thuộc tùy chọn không được bao gồm khi gói được cài đặt. Tuy nhiên, bằng cách thêm tên nhóm trong dấu ngoặc vuông khi chạy

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388, bạn có thể chỉ định thủ công rằng chúng sẽ được cài đặt. Ví dụ: bạn có thể cài đặt thêm phụ thuộc
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
472 của
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 bằng cách thực hiện như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
47

Bạn cũng có thể bao gồm các phụ thuộc tùy chọn khi ghim các phụ thuộc của mình với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
463 bằng cách sử dụng tùy chọn dòng lệnh
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
477

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
48

Điều này tạo ra một tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
468 được ghim bao gồm cả phụ thuộc thông thường và phụ thuộc phát triển của bạn

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

Ghi lại gói hàng của bạn

Bạn nên thêm một số tài liệu trước khi phát hành gói của mình ra thế giới. Tùy thuộc vào dự án của bạn, tài liệu của bạn có thể nhỏ như một tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
479 hoặc toàn diện như một trang web đầy đủ với các hướng dẫn, phòng trưng bày ví dụ và tham chiếu API

Ở mức tối thiểu, bạn nên đính kèm một tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
479 với dự án của mình. Một
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
479 tốt sẽ mô tả nhanh dự án của bạn, cũng như giải thích cách cài đặt và sử dụng gói của bạn. Thông thường, bạn muốn tham chiếu
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
479 của mình trong khóa
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
483 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45. Điều này cũng sẽ hiển thị thông tin trên trang dự án PyPI

Bạn có thể sử dụng Markdown hoặc reStructuredText làm định dạng cho mô tả dự án. PyPI tìm ra định dạng bạn đang sử dụng dựa trên phần mở rộng tệp. Nếu bạn không cần bất kỳ tính năng nâng cao nào của reStructuredText, thì tốt hơn hết bạn nên sử dụng Markdown cho

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
479 của mình. Nó đơn giản hơn và có nhiều hỗ trợ hơn bên ngoài PyPI

Đối với các dự án lớn hơn, bạn có thể muốn cung cấp nhiều tài liệu hơn mức có thể phù hợp một cách hợp lý trong một tệp. Trong trường hợp đó, bạn có thể lưu trữ tài liệu của mình trên các trang web như GitHub hoặc Đọc tài liệu và liên kết tới tài liệu đó từ trang dự án PyPI

Bạn có thể liên kết đến các URL khác bằng cách chỉ định chúng trong bảng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
420 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45. Trong ví dụ này, phần URL được sử dụng để liên kết đến kho lưu trữ GitHub của
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8

Kiểm tra gói hàng của bạn

Các bài kiểm tra rất hữu ích khi bạn đang phát triển gói của mình và bạn nên đưa chúng vào. Như đã lưu ý, bạn sẽ không đề cập đến kiểm thử trong hướng dẫn này, nhưng bạn có thể xem các kiểm thử của

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 trong thư mục mã nguồn
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
40

Bạn có thể tìm hiểu thêm về thử nghiệm trong Thử nghiệm Python hiệu quả với Pytest và có một số kinh nghiệm thực tế với phát triển dựa trên thử nghiệm [TDD] trong Xây dựng bảng băm trong Python với các vấn đề thực hành về TDD và Python. Phân tích tệp CSV

Khi chuẩn bị gói của bạn để xuất bản, bạn nên ý thức được vai trò của các bài kiểm tra. Chúng thường chỉ thú vị đối với các nhà phát triển, vì vậy chúng không nên được đưa vào gói mà bạn phân phối thông qua PyPI

Các phiên bản mới hơn của Setuptools khá tốt trong việc khám phá mã và thông thường sẽ bao gồm mã nguồn của bạn trong bản phân phối gói nhưng bỏ qua các bài kiểm tra, tài liệu và các tạo phẩm phát triển tương tự của bạn

Bạn có thể kiểm soát chính xác những gì có trong gói hàng của mình bằng cách sử dụng chỉ thị

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
491 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45. Xem tài liệu Setuptools để biết thêm thông tin

Phiên bản gói của bạn

Gói của bạn cần phải có một phiên bản. Hơn nữa, PyPI sẽ chỉ cho phép bạn tải lên một phiên bản gói cụ thể của mình một lần. Nói cách khác, nếu bạn muốn cập nhật gói của mình trên PyPI, thì trước tiên bạn cần tăng số phiên bản. Đây là một điều tốt vì nó giúp đảm bảo khả năng tái sản xuất. hai môi trường có cùng phiên bản của một gói nhất định sẽ hoạt động giống nhau

Có nhiều lược đồ phiên bản khác nhau mà bạn có thể sử dụng. Đối với các dự án Python, PEP 440 đưa ra một số đề xuất. Tuy nhiên, để linh hoạt, mô tả trong PEP đó rất phức tạp. Đối với một dự án đơn giản, bạn nên sử dụng sơ đồ lập phiên bản đơn giản

Phiên bản ngữ nghĩa là một sơ đồ mặc định tốt để sử dụng, mặc dù nó không hoàn hảo. Bạn chỉ định phiên bản dưới dạng ba thành phần số, ví dụ:

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
493. Các thành phần lần lượt được gọi là MAJOR, MINOR và PATCH. Sau đây là các khuyến nghị về thời điểm tăng từng thành phần

  • Tăng phiên bản CHÍNH khi bạn thực hiện các thay đổi API không tương thích
  • Tăng phiên bản MINOR khi bạn thêm chức năng theo cách tương thích ngược
  • Tăng phiên bản PATCH khi bạn sửa lỗi tương thích ngược. [Nguồn]

Bạn nên đặt lại PATCH thành

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
60 khi bạn tăng MINOR và đặt lại cả PATCH và MINOR thành
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
60 khi bạn tăng MAJOR

Phiên bản lịch là một giải pháp thay thế cho phiên bản ngữ nghĩa đang trở nên phổ biến và được sử dụng bởi các dự án như Ubuntu, Twisted, Black và

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388. Các phiên bản lịch cũng bao gồm một số thành phần số, nhưng một hoặc một số thành phần này được gắn với năm, tháng hoặc tuần hiện tại

Thông thường, bạn muốn chỉ định số phiên bản trong các tệp khác nhau trong dự án của mình. Ví dụ: số phiên bản được đề cập trong cả

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
498 trong gói
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8. Để giúp bạn đảm bảo rằng số phiên bản nhất quán, bạn có thể sử dụng một công cụ như BumpVer

BumpVer cho phép bạn ghi số phiên bản trực tiếp vào các tệp của mình và sau đó cập nhật chúng khi cần thiết. Ví dụ: bạn có thể cài đặt và tích hợp BumpVer vào dự án của mình như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
49

Lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
700 tạo một phần trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 của bạn cho phép bạn định cấu hình công cụ cho dự án của mình. Tùy thuộc vào nhu cầu của bạn, bạn có thể cần thay đổi nhiều cài đặt mặc định. Đối với
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8, bạn có thể kết thúc với một cái gì đó như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
70

Để BumpVer hoạt động bình thường, bạn phải chỉ định tất cả các tệp chứa số phiên bản của bạn trong tiểu mục

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
703. Lưu ý rằng BumpVer hoạt động tốt với Git và có thể tự động cam kết, gắn thẻ và đẩy khi bạn cập nhật số phiên bản

Ghi chú. BumpVer tích hợp với hệ thống kiểm soát phiên bản của bạn. Nó sẽ từ chối cập nhật các tệp của bạn nếu bạn có các thay đổi chưa được cam kết trong kho lưu trữ của mình

Sau khi thiết lập cấu hình, bạn có thể thêm phiên bản vào tất cả các tệp của mình bằng một lệnh duy nhất. Ví dụ: để tăng phiên bản MINOR của

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8, bạn sẽ làm như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
71

Điều này thay đổi số phiên bản từ 1. 0. 0 đến 1. 1. 0 trong cả

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
307. Bạn có thể sử dụng cờ
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
707 để xem BumpVer sẽ thực hiện những thay đổi nào mà không thực sự thực hiện chúng

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

Thêm tệp tài nguyên vào gói của bạn

Đôi khi, bạn sẽ có các tệp bên trong gói không phải là tệp mã nguồn. Các ví dụ bao gồm tệp dữ liệu, tệp nhị phân, tài liệu và—như bạn có trong ví dụ này—tệp cấu hình

Để đảm bảo các tệp như vậy được bao gồm khi dự án của bạn được tạo, bạn sử dụng tệp kê khai. Đối với nhiều dự án, bạn không cần phải lo lắng về bảng kê khai. theo mặc định, Setuptools bao gồm tất cả các tệp mã nguồn và tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
479 trong bản dựng

Nếu bạn có các tệp tài nguyên khác và cần cập nhật tệp kê khai, thì bạn cần tạo một tệp có tên

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
43 bên cạnh
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 trong thư mục cơ sở của dự án của bạn. Tệp này chỉ định các quy tắc bao gồm tệp nào và loại trừ tệp nào

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
72

Ví dụ này sẽ bao gồm tất cả các tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
711 trong thư mục
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
8. Trong thực tế, đây là tập tin cấu hình

Xem tài liệu để biết thêm thông tin về cách thiết lập bảng kê khai của bạn. Công cụ check-manifest cũng có thể hữu ích khi làm việc với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
43

Cấp phép cho gói của bạn

Nếu bạn đang chia sẻ gói của mình với người khác thì bạn cần thêm giấy phép vào gói giải thích cách người khác được phép sử dụng gói của bạn. Ví dụ:

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 được phân phối theo giấy phép MIT

Giấy phép là tài liệu pháp lý và bạn thường không muốn viết giấy phép của riêng mình. Thay vào đó, bạn nên chọn một trong nhiều giấy phép đã có sẵn

Bạn nên thêm một tệp có tên

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
42 vào dự án của mình có chứa nội dung giấy phép bạn chọn. Sau đó, bạn có thể tham khảo tệp này trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 để hiển thị giấy phép trên PyPI

Cài đặt gói của bạn cục bộ

Bạn đã hoàn thành tất cả các thiết lập và cấu hình cần thiết cho gói của mình. Trong phần tiếp theo, bạn sẽ tìm hiểu cách cuối cùng nhận được gói của mình trên PyPI. Tuy nhiên, trước tiên, bạn sẽ tìm hiểu về các lượt cài đặt có thể chỉnh sửa. Đây là một cách sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 để cài đặt cục bộ gói của bạn theo cách cho phép bạn chỉnh sửa mã của mình sau khi cài đặt

Ghi chú. Thông thường,

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 thực hiện cài đặt thông thường, đặt gói vào thư mục
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
719 của bạn. Nếu bạn cài đặt dự án cục bộ của mình thì mã nguồn sẽ được sao chép sang
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
719. Ảnh hưởng của việc này là những thay đổi sau này mà bạn thực hiện sẽ không có hiệu lực. Trước tiên, bạn cần cài đặt lại gói của mình

Trong quá trình phát triển, điều này có thể không hiệu quả và gây khó chịu. Các bản cài đặt có thể chỉnh sửa giải quyết vấn đề này bằng cách liên kết trực tiếp với mã nguồn của bạn

Cài đặt có thể chỉnh sửa đã được chính thức hóa trong PEP 660. Chúng rất hữu ích khi bạn đang phát triển gói của mình, vì bạn có thể kiểm tra tất cả chức năng của gói và cập nhật mã nguồn của mình mà không cần thực hiện cài đặt lại

Bạn sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 để cài đặt gói của mình ở chế độ có thể chỉnh sửa bằng cách thêm cờ
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
722 hoặc
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
723

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
3

Lưu ý khoảng thời gian [

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
724] ở cuối lệnh. Đó là một phần cần thiết của lệnh và nói với
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 rằng bạn muốn cài đặt gói nằm trong thư mục làm việc hiện tại. Nói chung, đây phải là đường dẫn đến thư mục chứa tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 của bạn

Ghi chú. Bạn có thể nhận được thông báo lỗi cho biết “Tệp dự án có tệp ‘pyproject. toml' và chương trình phụ trợ xây dựng của nó thiếu hook 'build_editable'. ” Điều này là do giới hạn trong hỗ trợ của Setuptools cho PEP 660. Bạn có thể giải quyết vấn đề này bằng cách thêm một tệp có tên

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
405 với các nội dung sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
74

Miếng chêm này ủy thác công việc thực hiện cài đặt có thể chỉnh sửa cho cơ chế kế thừa của Setuptools cho đến khi có hỗ trợ riêng cho PEP 660

Khi bạn đã cài đặt thành công dự án của mình, nó sẽ có sẵn trong môi trường của bạn, không phụ thuộc vào thư mục hiện tại của bạn. Ngoài ra, tập lệnh của bạn được thiết lập để bạn có thể chạy chúng. Nhớ lại rằng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 đã định nghĩa một tập lệnh có tên là
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
422

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
75

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

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
67 từ bất kỳ thư mục nào hoặc nhập gói của mình từ REPL hoặc tập lệnh khác

>>>

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
76

Cài đặt gói của bạn ở chế độ có thể chỉnh sửa trong quá trình phát triển giúp trải nghiệm phát triển của bạn dễ chịu hơn nhiều. Đây cũng là một cách tốt để xác định một số lỗi mà bạn có thể vô tình phụ thuộc vào các tệp có sẵn trong thư mục làm việc hiện tại của bạn

Quá trình này mất một chút thời gian, nhưng điều này kết thúc quá trình chuẩn bị mà bạn cần thực hiện đối với gói hàng của mình. Trong phần tiếp theo, bạn sẽ học cách thực sự xuất bản nó

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

Xuất bản gói của bạn lên PyPI

Gói của bạn cuối cùng đã sẵn sàng để gặp thế giới bên ngoài máy tính của bạn. Trong phần này, bạn sẽ tìm hiểu cách xây dựng gói của mình và tải gói đó lên PyPI

Nếu bạn chưa có tài khoản trên PyPI, thì bây giờ là lúc để đăng ký tài khoản của bạn trên PyPI. Trong khi bạn đang ở đó, bạn cũng nên đăng ký một tài khoản trên TestPyPI. TestPyPI rất hữu ích. Bạn có thể thử tất cả các bước xuất bản một gói mà không gặp bất kỳ hậu quả nào nếu bạn làm sai

Để xây dựng và tải gói của bạn lên PyPI, bạn sẽ sử dụng hai công cụ có tên là Build và Twine. Bạn có thể cài đặt chúng bằng cách sử dụng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 như bình thường

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
77

Bạn sẽ học cách sử dụng những công cụ này trong các phần phụ sắp tới

Xây dựng gói của bạn

Các gói trên PyPI không được phân phối dưới dạng mã nguồn đơn giản. Thay vào đó, chúng được gói gọn trong các gói phân phối. Các định dạng phổ biến nhất cho các gói phân phối là kho lưu trữ nguồn và bánh xe Python

Ghi chú. Bánh xe được đặt tên theo bánh pho mát, là mặt hàng quan trọng nhất trong cửa hàng pho mát

Kho lưu trữ nguồn bao gồm mã nguồn của bạn và mọi tệp hỗ trợ được gói gọn trong một tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
732. Tương tự, bánh xe về cơ bản là một kho lưu trữ zip chứa mã của bạn. Bạn nên cung cấp cả kho lưu trữ nguồn và bánh xe cho gói của mình. Bánh xe thường nhanh hơn và thuận tiện hơn cho người dùng cuối của bạn, trong khi kho lưu trữ nguồn cung cấp giải pháp thay thế sao lưu linh hoạt

Để tạo kho lưu trữ nguồn và bánh xe cho gói của bạn, bạn sử dụng Build

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
78

Như dòng cuối cùng trong đầu ra cho biết, điều này tạo ra một kho lưu trữ nguồn và một bánh xe. Bạn có thể tìm thấy chúng trong thư mục

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
733 mới được tạo

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
79

Tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
734 là kho lưu trữ nguồn của bạn, trong khi tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
735 là bánh xe của bạn. Đây là những tệp mà bạn sẽ tải lên PyPI và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388 sẽ tải xuống khi cài đặt gói của bạn sau này

Xác nhận bản dựng gói của bạn

Trước khi tải lên các gói phân phối mới xây dựng của mình, bạn nên kiểm tra xem chúng có chứa các tệp mà bạn mong đợi không. Tệp bánh xe thực sự là một tệp ZIP có phần mở rộng khác. Bạn có thể giải nén nó và kiểm tra nội dung của nó như sau

  • các cửa sổ
  • Linux + macOS

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
00

Trước tiên, bạn đổi tên tệp bánh xe để có phần mở rộng

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
737 để bạn có thể mở rộng nó

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
01

Bạn sẽ thấy tất cả mã nguồn của mình được liệt kê, cũng như một số tệp mới đã được tạo và chứa thông tin bạn đã cung cấp trong

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45. Đặc biệt, đảm bảo rằng tất cả các gói phụ và tệp hỗ trợ như
# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
70 đều được bao gồm

Bạn cũng có thể xem bên trong kho lưu trữ nguồn vì nó được đóng gói dưới dạng quả bóng tar. Tuy nhiên, nếu bánh xe của bạn chứa các tệp bạn mong đợi, thì kho lưu trữ nguồn cũng sẽ ổn

Twine cũng có thể kiểm tra xem mô tả gói của bạn có hiển thị chính xác trên PyPI không. Bạn có thể chạy

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
740 trên các tệp được tạo trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
733

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
02

Điều này sẽ không nắm bắt được tất cả các vấn đề mà bạn có thể gặp phải, nhưng đó là tuyến phòng thủ đầu tiên tốt

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

Tải lên gói của bạn

Bây giờ bạn đã sẵn sàng để thực sự tải gói của mình lên PyPI. Đối với điều này, bạn sẽ lại sử dụng công cụ Twine, yêu cầu nó tải lên các gói phân phối mà bạn đã tạo

Trước tiên, bạn nên tải lên TestPyPI để đảm bảo mọi thứ hoạt động như mong đợi

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
03

Twine sẽ hỏi bạn tên người dùng và mật khẩu của bạn

Ghi chú. Nếu bạn đã làm theo hướng dẫn bằng cách sử dụng gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8 làm ví dụ, thì lệnh trước đó có thể sẽ không thành công với thông báo cho biết bạn không được phép tải lên dự án
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
313

Bạn có thể thay đổi

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
411 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 thành một cái gì đó độc đáo, ví dụ như
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
746. Sau đó xây dựng lại dự án và tải các tệp mới được tạo lên TestPyPI

Nếu quá trình tải lên thành công, thì bạn có thể nhanh chóng truy cập TestPyPI, cuộn xuống và xem dự án của bạn được hiển thị một cách tự hào giữa các bản phát hành mới. Nhấp vào gói hàng của bạn và đảm bảo mọi thứ đều ổn

Nếu bạn đang theo dõi bằng cách sử dụng gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8, hướng dẫn này sẽ kết thúc tại đây. Mặc dù bạn có thể chơi với TestPyPI bao nhiêu tùy thích, nhưng bạn không nên tải các gói mẫu lên PyPI chỉ để thử nghiệm

Ghi chú. TestPyPI là công cụ tuyệt vời để kiểm tra xem gói của bạn có tải lên chính xác không và trang dự án của bạn trông như bạn dự định. Bạn cũng có thể thử cài đặt gói của mình từ TestPyPI

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
04

Tuy nhiên, lưu ý rằng điều này có thể thất bại vì không phải tất cả các phụ thuộc của bạn đều có sẵn trên TestPyPI. Đây không phải là một vấn đề. Gói của bạn vẫn hoạt động khi bạn tải nó lên PyPI

Nếu bạn có gói của riêng mình để xuất bản, thì thời điểm cuối cùng đã đến. Với tất cả các bước chuẩn bị được thực hiện, bước cuối cùng này rất ngắn

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
05

Cung cấp tên người dùng và mật khẩu của bạn khi được yêu cầu. Đó là nó

Truy cập PyPI và tra cứu gói của bạn. Bạn có thể tìm thấy nó bằng cách tìm kiếm, bằng cách xem trang Dự án của bạn hoặc bằng cách truy cập trực tiếp vào URL của dự án của bạn. pypi. org/dự án/tên-gói-của-bạn/

Xin chúc mừng. Gói của bạn được xuất bản trên PyPI

Cài đặt gói của bạn

Hãy dành một chút thời gian để đắm mình trong ánh sáng xanh của trang web PyPI và khoe khoang với bạn bè của bạn

Sau đó mở lại một thiết bị đầu cuối. Có thêm một phần thưởng tuyệt vời

Với gói của bạn được tải lên PyPI, bạn cũng có thể cài đặt gói đó với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388. Đầu tiên, tạo một môi trường ảo mới và kích hoạt nó. Sau đó chạy lệnh sau

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
06

Thay thế

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
749 bằng tên mà bạn đã chọn cho gói hàng của mình. Chẳng hạn, để cài đặt gói
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8, bạn sẽ làm như sau

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
07

Nhìn thấy mã của riêng bạn được cài đặt bởi

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388—giống như bất kỳ thư viện bên thứ ba nào khác—là một cảm giác tuyệt vời

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

Khám phá các hệ thống xây dựng khác

Trong hướng dẫn này, bạn đã sử dụng Setuptools để xây dựng gói của mình. Setuptools, dù tốt hay xấu, là tiêu chuẩn dài hạn để tạo các gói. Mặc dù nó phổ biến và đáng tin cậy, nhưng nó cũng đi kèm với rất nhiều tính năng có thể không liên quan đến bạn

Có một số hệ thống xây dựng thay thế mà bạn có thể sử dụng thay vì Setuptools. Trong vài năm qua, cộng đồng Python đã thực hiện công việc quan trọng là chuẩn hóa hệ sinh thái đóng gói Python. Điều này giúp việc di chuyển giữa các hệ thống bản dựng khác nhau trở nên đơn giản hơn và sử dụng hệ thống phù hợp nhất với quy trình làm việc và các gói của bạn

Trong phần này, bạn sẽ tìm hiểu ngắn gọn về hai hệ thống xây dựng thay thế mà bạn có thể sử dụng để tạo và xuất bản các gói Python của mình. Ngoài Flit và Thơ mà bạn sẽ tìm hiểu tiếp theo, bạn cũng có thể xem pbr, enscons và Hatchling. Ngoài ra, gói

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
752 cung cấp hỗ trợ để tạo hệ thống xây dựng của riêng bạn

bay

Flit là một dự án nhỏ tuyệt vời nhằm mục đích “làm cho mọi thứ dễ dàng trở nên dễ dàng” khi nói đến bao bì [nguồn]. Flit không hỗ trợ các gói nâng cao như các gói tạo tiện ích mở rộng C và nói chung, nó không cung cấp cho bạn nhiều lựa chọn khi thiết lập gói của bạn. Thay vào đó, Flit ủng hộ triết lý rằng phải có một quy trình làm việc rõ ràng để xuất bản một gói

Ghi chú. Bạn không thể định cấu hình gói của mình bằng cả Setuptools và Flit cùng một lúc. Để kiểm tra quy trình làm việc trong phần này, bạn nên lưu trữ cấu hình Setuptools một cách an toàn trong hệ thống kiểm soát phiên bản của mình, sau đó xóa các phần

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
402 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
754 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45

Đầu tiên cài đặt Flit với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
08

Nhiều nhất có thể, Flit tự động hóa các bước chuẩn bị mà bạn cần thực hiện với gói hàng của mình. Để bắt đầu định cấu hình gói mới, hãy chạy

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
757

# __init__.py

from importlib import resources
try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib

# Version of the realpython-reader package
__version__ = "1.0.0"

# Read URL of the Real Python feed from config file
_cfg = tomllib.loads[resources.read_text["reader", "config.toml"]]
URL = _cfg["feed"]["url"]
09

Lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
757 sẽ tạo tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 dựa trên câu trả lời mà bạn đưa ra cho một số câu hỏi. Bạn có thể cần chỉnh sửa tệp này một chút trước khi sử dụng nó. Đối với dự án
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
8, tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 cho Flit kết thúc như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
0

Lưu ý rằng hầu hết các mục

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
754 đều giống với mục gốc của bạn
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45. Tuy nhiên, có một điểm khác biệt là
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
412 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
434 được chỉ định trong trường
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
766. Flit thực sự tự tìm ra những điều này bằng cách sử dụng
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
310 và chuỗi tài liệu được xác định trong tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
307. Tài liệu của Flit giải thích mọi thứ về tệp
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45

Flit có thể xây dựng gói của bạn và xuất bản lên PyPI. Bạn không cần sử dụng Build and Twine. Để xây dựng gói của bạn, chỉ cần làm như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
1

Điều này tạo ra một kho lưu trữ nguồn và một bánh xe, tương tự như những gì bạn đã làm với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
770 trước đó. Bạn vẫn có thể sử dụng Build nếu bạn thích

Để tải gói của bạn lên PyPI, bạn có thể sử dụng Twine như bạn đã làm trước đó. Tuy nhiên, bạn cũng có thể sử dụng Flit trực tiếp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
2

Lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
771 sẽ xây dựng gói của bạn nếu cần, sau đó tải các tệp lên PyPI, nhắc bạn nhập tên người dùng và mật khẩu của mình

Để xem phiên bản ban đầu nhưng dễ nhận biết của Flit đang hoạt động, hãy xem cuộc nói chuyện chớp nhoáng của Thomas Kluyver từ EuroSciPy 2017. Bản trình diễn cho thấy cách bạn định cấu hình gói của mình, xây dựng và xuất bản gói đó lên PyPI trong khoảng thời gian hai phút

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

Thơ

Thơ là một công cụ khác mà bạn có thể sử dụng để xây dựng và tải lên gói của mình. So với Flit, Poetry có nhiều tính năng hơn có thể giúp bạn trong quá trình phát triển các gói của mình, bao gồm quản lý phụ thuộc mạnh mẽ

Trước khi sử dụng Thơ, bạn cần cài đặt nó. Có thể cài đặt Thơ với

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
388. Tuy nhiên, những người bảo trì khuyên bạn nên sử dụng tập lệnh cài đặt tùy chỉnh để tránh xung đột phụ thuộc tiềm ẩn. Xem tài liệu hướng dẫn

Ghi chú. Bạn không thể định cấu hình gói của mình bằng cả Công cụ thiết lập và Thơ cùng một lúc. Để kiểm tra quy trình làm việc trong phần này, bạn nên lưu trữ cấu hình Setuptools một cách an toàn trong hệ thống kiểm soát phiên bản của mình, sau đó xóa các phần

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
402 và
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
754 trong
 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45

Sau khi cài đặt Poetry, bạn bắt đầu sử dụng bằng lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
776, tương tự như Flit

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
3

Thao tác này sẽ tạo tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 dựa trên câu trả lời của bạn cho các câu hỏi về gói hàng của mình

Ghi chú. Thơ hiện không hỗ trợ PEP 621, vì vậy các thông số kỹ thuật thực tế bên trong

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 hiện có sự khác biệt giữa Thơ và các công cụ khác

Đối với Thơ, tệp

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45 kết thúc như sau

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
4

Bạn nên nhận ra tất cả các mục này từ cuộc thảo luận trước đó về

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
45, mặc dù các phần được đặt tên khác nhau

Một điều cần lưu ý là Thơ sẽ tự động thêm các bộ phân loại dựa trên giấy phép và phiên bản Python mà bạn chỉ định. Thơ cũng yêu cầu bạn phải rõ ràng về các phiên bản phụ thuộc của bạn. Trên thực tế, quản lý phụ thuộc là một trong những điểm mạnh của Thơ

Cũng giống như Flit, Poetry có thể xây dựng và tải các gói lên PyPI. Lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
781 tạo kho lưu trữ nguồn và bánh xe

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
5

Thao tác này sẽ tạo hai tệp thông thường trong thư mục con

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
733 mà bạn có thể tải lên bằng Twine như trước đó. Bạn cũng có thể sử dụng Thơ để xuất bản lên PyPI

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
6

Điều này sẽ tải gói của bạn lên PyPI. Ngoài việc hỗ trợ xây dựng và xuất bản, Thơ có thể giúp bạn sớm hơn trong quá trình này. Thơ có thể giúp bạn bắt đầu một dự án mới bằng lệnh

 1# __main__.py
 2
 3import sys
 4
 5from reader import feed, viewer
 6
 7def main[]:
 8    """Read the Real Python article feed"""
 9
10    # If an article ID is given, then show the article
11    if len[sys.argv] > 1:
12        article = feed.get_article[sys.argv[1]]
13        viewer.show[article]
14
15    # If no ID is given, then show a list of all articles
16    else:
17        site = feed.get_site[]
18        titles = feed.get_titles[]
19        viewer.show_list[site, titles]
20
21if __name__ == "__main__":
22    main[]
783. Nó cũng hỗ trợ làm việc với môi trường ảo. Xem tài liệu của Thơ để biết tất cả các chi tiết

Ngoài các tệp cấu hình hơi khác nhau, Flit và Thơ hoạt động rất giống nhau. Thơ có phạm vi rộng hơn, vì nó cũng nhằm mục đích giúp quản lý sự phụ thuộc, trong khi Flit đã tồn tại lâu hơn một chút

Sự kết luận

Bây giờ bạn đã biết cách chuẩn bị dự án của mình và tải nó lên PyPI để người khác có thể cài đặt và sử dụng nó. Mặc dù có một vài bước mà bạn cần thực hiện, nhưng nhìn thấy gói của riêng bạn trên PyPI là một phần thưởng tuyệt vời. Có những người khác thấy dự án của bạn hữu ích thậm chí còn tốt hơn

Trong hướng dẫn này, bạn đã học cách xuất bản gói của riêng mình bằng cách

  • Tìm một cái tên hay cho gói hàng của bạn
  • Định cấu hình gói của bạn bằng cách sử dụng
     1# __main__.py
     2
     3import sys
     4
     5from reader import feed, viewer
     6
     7def main[]:
     8    """Read the Real Python article feed"""
     9
    10    # If an article ID is given, then show the article
    11    if len[sys.argv] > 1:
    12        article = feed.get_article[sys.argv[1]]
    13        viewer.show[article]
    14
    15    # If no ID is given, then show a list of all articles
    16    else:
    17        site = feed.get_site[]
    18        titles = feed.get_titles[]
    19        viewer.show_list[site, titles]
    20
    21if __name__ == "__main__":
    22    main[]
    
    45
  • Xây dựng gói của bạn
  • Đang tải gói của bạn lên PyPI

Ngoài ra, bạn đã tìm hiểu về các sáng kiến ​​trong cộng đồng đóng gói Python để chuẩn hóa các công cụ và quy trình

Nếu bạn vẫn còn thắc mắc, vui lòng liên hệ trong phần bình luận bên dưới. Ngoài ra, Hướng dẫn sử dụng Bao bì Python có nhiều thông tin chi tiết hơn những gì bạn đã thấy trong hướng dẫn này

Nhận mã nguồn. Nhấp vào đây để có quyền truy cập vào mã nguồn của Real Python Feed Reader mà bạn sẽ làm việc trong hướng dẫn này

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

Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. Cách xuất bản gói Python của riêng bạn lên PyPI

🐍 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

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

Giới thiệu về Geir Arne Hjelle

Geir Arne là một Pythonista cuồng nhiệt và là thành viên của nhóm hướng dẫn Real Python

» Thông tin thêm về Geir Arne

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à

Adriana

Bartosz

David

Joanna

kate

Nelson

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 Pythonista chuyên gia

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

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 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. Nhận các mẹo để đặt câu hỏi hay 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

Chúng tôi có thể tạo các gói của riêng mình bằng Python không?

Gói Python của bạn phải có một tên duy nhất . Không có hai gói Python hiện có nào có thể có cùng tên. Thư mục mẹ và thư mục con phải có cùng tên, đó là tên của gói của bạn. Bên trong thiết lập. tệp py có tên tham số và gói.

Gói trong ví dụ Python là gì?

Một gói về cơ bản là một thư mục chứa các tệp Python và một tệp có tên __init__. py . Điều này có nghĩa là mọi thư mục bên trong đường dẫn Python chứa tệp có tên __init__. py, sẽ được Python coi là một gói.

Chủ Đề