Hướng dẫn what is the use of modules and packages in python? - việc sử dụng các mô-đun và gói trong python là gì?

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: các mô -đun và gói Python: Giới thiệu This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Modules and Packages: An Introduction

Show

Bài viết này khám phá các mô -đun Python và các gói Python, hai cơ chế tạo điều kiện cho lập trình mô -đun.modules and Python packages, two mechanisms that facilitate modular programming.

Lập trình mô -đun đề cập đến quá trình phá vỡ một nhiệm vụ lập trình lớn, khó sử dụng thành các nhiệm vụ hoặc mô -đun riêng biệt, nhỏ hơn, dễ quản lý hơn. Các mô -đun riêng lẻ sau đó có thể được ghép lại với nhau như các khối xây dựng để tạo ra một ứng dụng lớn hơn. refers to the process of breaking a large, unwieldy programming task into separate, smaller, more manageable subtasks or modules. Individual modules can then be cobbled together like building blocks to create a larger application.

Có một số lợi thế để mô đun hóa mã trong một ứng dụng lớn:modularizing code in a large application:

  • Đơn giản: Thay vì tập trung vào toàn bộ vấn đề trong tay, một mô -đun thường tập trung vào một phần tương đối nhỏ của vấn đề. Nếu bạn làm việc trên một mô -đun duy nhất, bạn sẽ có một miền vấn đề nhỏ hơn để quấn đầu. Điều này làm cho sự phát triển dễ dàng hơn và dễ bị lỗi hơn. Rather than focusing on the entire problem at hand, a module typically focuses on one relatively small portion of the problem. If you’re working on a single module, you’ll have a smaller problem domain to wrap your head around. This makes development easier and less error-prone.

  • Khả năng duy trì: Các mô -đun thường được thiết kế để chúng thực thi các ranh giới logic giữa các lĩnh vực vấn đề khác nhau. Nếu các mô -đun được viết theo cách giảm thiểu sự phụ thuộc lẫn nhau, thì khả năng các sửa đổi thành một mô -đun sẽ có tác động đến các phần khác của chương trình. . Modules are typically designed so that they enforce logical boundaries between different problem domains. If modules are written in a way that minimizes interdependency, there is decreased likelihood that modifications to a single module will have an impact on other parts of the program. (You may even be able to make changes to a module without having any knowledge of the application outside that module.) This makes it more viable for a team of many programmers to work collaboratively on a large application.

  • Khả năng tái sử dụng: Chức năng được xác định trong một mô -đun duy nhất có thể được sử dụng lại (thông qua giao diện được xác định phù hợp) bởi các phần khác của ứng dụng. Điều này loại bỏ sự cần thiết phải sao chép mã. Functionality defined in a single module can be easily reused (through an appropriately defined interface) by other parts of the application. This eliminates the need to duplicate code.

  • Phạm vi: Các mô -đun thường xác định một không gian tên riêng biệt, giúp tránh va chạm giữa các định danh trong các khu vực khác nhau của chương trình. . Modules typically define a separate namespace, which helps avoid collisions between identifiers in different areas of a program. (One of the tenets in the Zen of Python is Namespaces are one honking great idea—let’s do more of those!)

Các chức năng, mô -đun và gói đều là tất cả các cấu trúc trong Python nhằm thúc đẩy mô đun hóa mã., modules and packages are all constructs in Python that promote code modularization.

Các mô -đun Python: Tổng quan

Thực tế có ba cách khác nhau để xác định một mô -đun trong Python:module in Python:

  1. Một mô -đun có thể được viết bằng chính Python.
  2. Một mô-đun có thể được viết bằng C và được tải động trong thời gian chạy, như mô-đun
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    4 (biểu thức chính quy).C and loaded dynamically at run-time, like the
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    4 (regular expression) module.
  3. Một mô-đun tích hợp thực chất được chứa trong trình thông dịch, như mô-đun
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    5.built-in module is intrinsically contained in the interpreter, like the
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    5 module.

Một nội dung mô -đun được truy cập theo cùng một cách trong cả ba trường hợp: với câu lệnh

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6.

Ở đây, trọng tâm chủ yếu sẽ là các mô -đun được viết bằng Python. Điều thú vị về các mô -đun được viết bằng Python là chúng cực kỳ đơn giản để xây dựng. Tất cả những gì bạn cần làm là tạo một tệp chứa mã Python hợp pháp và sau đó đặt cho tệp một tên với tiện ích mở rộng

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
7. Đó là nó! Không có cú pháp đặc biệt hoặc voodoo là cần thiết.

Ví dụ: giả sử bạn đã tạo một tệp có tên

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8 chứa các mục sau:

mod.py

s = "If Comrade Napoleon says it, it must be right."
a = [100, 200, 300]

def foo(arg):
    print(f'arg = {arg}')

class Foo:
    pass

Một số đối tượng được xác định trong

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8:

  • import <module_name>[, <module_name> ...]
    
    0 (một chuỗi)
  • import <module_name>[, <module_name> ...]
    
    1 (một danh sách)
  • import <module_name>[, <module_name> ...]
    
    2 (một hàm)
  • import <module_name>[, <module_name> ...]
    
    3 (một lớp)

Giả sử

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8 ở một vị trí thích hợp, bạn sẽ tìm hiểu thêm về thời gian ngắn, các đối tượng này có thể được truy cập bằng cách nhập mô -đun như sau:importing the module as follows:

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

Đường dẫn tìm kiếm mô -đun

Tiếp tục với ví dụ trên, chúng ta hãy xem xét những gì xảy ra khi Python thực hiện tuyên bố:

Khi trình thông dịch thực thi câu lệnh

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 trên, nó sẽ tìm kiếm
>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8 trong danh sách các thư mục được lắp ráp từ các nguồn sau:

  • Thư mục mà tập lệnh đầu vào được chạy hoặc thư mục hiện tại nếu trình thông dịch được chạy tương táccurrent directory if the interpreter is being run interactively
  • Danh sách các thư mục có trong biến môi trường
    import <module_name>[, <module_name> ...]
    
    7, nếu nó được đặt. (Định dạng cho
    import <module_name>[, <module_name> ...]
    
    7 phụ thuộc OS nhưng nên bắt chước biến môi trường
    import <module_name>[, <module_name> ...]
    
    9.)
  • Danh sách các thư mục phụ thuộc vào cài đặt được cấu hình tại thời điểm Python được cài đặt

Đường dẫn tìm kiếm kết quả có thể truy cập được trong biến Python

from <module_name> import <name(s)>
0, được lấy từ một mô -đun có tên
from <module_name> import <name(s)>
1:

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']

Đường dẫn tìm kiếm mô -đun

  • Tiếp tục với ví dụ trên, chúng ta hãy xem xét những gì xảy ra khi Python thực hiện tuyên bố:current directory, if interactive
  • Khi trình thông dịch thực thi câu lệnh
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    6 trên, nó sẽ tìm kiếm
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    8 trong danh sách các thư mục được lắp ráp từ các nguồn sau:
    • Thư mục mà tập lệnh đầu vào được chạy hoặc thư mục hiện tại nếu trình thông dịch được chạy tương tác Put
      >>> mod.s
      'If Comrade Napoleon says it, it must be right.'
      >>> mod.foo('quux')
      arg = quux
      
      8 in one of the directories already contained in the
      import <module_name>[, <module_name> ...]
      
      7 variable
  • Danh sách các thư mục có trong biến môi trường
    import <module_name>[, <module_name> ...]
    
    7, nếu nó được đặt. (Định dạng cho
    import <module_name>[, <module_name> ...]
    
    7 phụ thuộc OS nhưng nên bắt chước biến môi trường
    import <module_name>[, <module_name> ...]
    
    9.)

Thực tế có một tùy chọn bổ sung: bạn có thể đặt tệp mô-đun vào bất kỳ thư mục nào bạn chọn và sau đó sửa đổi

from <module_name> import <name(s)>
0 vào thời gian chạy để nó chứa thư mục đó. Ví dụ: trong trường hợp này, bạn có thể đặt
>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8 vào thư mục
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

00 và sau đó đưa ra các câu sau:

>>>

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod

Khi một mô -đun đã được nhập, bạn có thể xác định vị trí được tìm thấy với thuộc tính mô -đun ____ ____101:

>>>

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'

Khi một mô -đun đã được nhập, bạn có thể xác định vị trí được tìm thấy với thuộc tính mô -đun ____ ____101:

Phần thư mục của >>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 01 nên là một trong những thư mục trong from import 0.

Tuyên bố

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 contents are made available to the caller with the
>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 statement. The
>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 statement takes many different forms, shown below.

>>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 07

Nội dung mô -đun được cung cấp cho người gọi với câu lệnh

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6. Tuyên bố
>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 có nhiều hình thức khác nhau, được hiển thị bên dưới.

Hình thức đơn giản nhất là mẫu đã được hiển thị ở trên:private symbol table, which serves as the global symbol table for all objects defined in the module. Thus, a module creates a separate namespace, as already noted.

Lưu ý rằng điều này không làm cho nội dung mô -đun có thể truy cập trực tiếp vào người gọi. Mỗi mô -đun có bảng ký hiệu riêng, đóng vai trò là bảng ký hiệu toàn cầu cho tất cả các đối tượng được xác định trong mô -đun. Do đó, một mô -đun tạo ra một không gian tên riêng, như đã lưu ý.

Tuyên bố

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

07 chỉ đặt
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

09 trong bảng biểu tượng người gọi. Các đối tượng được xác định trong mô -đun vẫn còn trong bảng biểu tượng riêng của mô -đun.dot notation, as illustrated below.

Từ người gọi, các đối tượng trong mô -đun chỉ có thể truy cập khi có tiền tố với

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

09 thông qua ký hiệu DOT, như được minh họa dưới đây.

>>>

>>> import mod
>>> mod

Sau tuyên bố

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 sau,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

12 được đặt vào bảng biểu tượng cục bộ. Do đó,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

12 có ý nghĩa trong bối cảnh địa phương của người gọi:

>>>

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined

Nhưng

import <module_name>[, <module_name> ...]
0 và
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

15 vẫn còn trong bảng biểu tượng riêng của mô -đun và không có ý nghĩa trong bối cảnh địa phương:

>>>

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux

Để được truy cập trong bối cảnh cục bộ, tên của các đối tượng được xác định trong mô -đun phải được đặt trước bằng

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

12:

import <module_name>[, <module_name> ...]

>>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 18

Một số mô-đun được phân tách bằng dấu phẩy có thể được chỉ định trong một câu lệnh

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6:

from <module_name> import <name(s)>

Một hình thức thay thế của câu lệnh

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 cho phép các đối tượng riêng lẻ từ mô -đun được nhập trực tiếp vào bảng biểu tượng người gọi:

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

0

Sau khi thực hiện câu lệnh trên,

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

20 có thể được tham chiếu trong môi trường người gọi mà không có tiền tố ____109:

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

1

Bởi vì hình thức

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 này đặt tên đối tượng trực tiếp vào bảng biểu tượng người gọi, bất kỳ đối tượng nào đã tồn tại với cùng tên sẽ bị ghi đè:

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

2

Thậm chí có thể bừa bãi

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 tất cả mọi thứ từ một mô -đun tại một cú đánh rơi:

Điều này sẽ đặt tên của tất cả các đối tượng từ

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

09 vào bảng ký hiệu cục bộ, ngoại trừ bất kỳ ký tự bắt đầu với ký tự dấu gạch dưới (
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

25).

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

3

Ví dụ:

>>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 26

Đây không nhất thiết phải được khuyến nghị trong mã sản xuất quy mô lớn. Nó có một chút nguy hiểm vì bạn đang nhập tên vào bảng biểu tượng địa phương. Trừ khi bạn biết rõ tất cả và có thể tự tin rằng đã giành chiến thắng, bạn có một cơ hội tốt để ghi đè một cái tên hiện có vô tình. Tuy nhiên, cú pháp này khá tiện dụng khi bạn chỉ đang loay hoay với trình thông dịch tương tác, cho mục đích thử nghiệm hoặc khám phá, bởi vì nó nhanh chóng cho phép bạn truy cập vào mọi thứ mà một mô -đun cung cấp mà không cần gõ nhiều.

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

4

Cũng có thể

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 đối tượng riêng lẻ nhưng nhập chúng vào bảng biểu tượng cục bộ với các tên thay thế:

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

5

>>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 28

Điều này cho phép đặt tên trực tiếp vào bảng biểu tượng cục bộ nhưng tránh xung đột với các tên hiện có trước đó:

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

6

>>>

Bạn cũng có thể nhập toàn bộ mô -đun dưới một tên thay thế:

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

7

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

8

Nội dung mô -đun có thể được nhập từ trong một định nghĩa hàm. Trong trường hợp đó,

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
6 không xảy ra cho đến khi hàm được gọi:Python 3 does not allow the indiscriminate
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 syntax from within a function:

>>>

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

9

Tuy nhiên, Python 3 không cho phép cú pháp

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 bừa bãi từ trong một hàm:

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
0

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
1

Cuối cùng, một tuyên bố >>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 31 với mệnh đề >>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 32 có thể được sử dụng để bảo vệ chống lại các nỗ lực >>> mod.s 'If Comrade Napoleon says it, it must be right.' >>> mod.foo('quux') arg = quux 6 không thành công:

Hàm

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

34local symbol table:

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
2

Hàm tích hợp

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

34 trả về danh sách các tên được xác định trong không gian tên. Không có các đối số, nó tạo ra một danh sách tên được sắp xếp theo thứ tự bảng chữ cái trong bảng biểu tượng cục bộ hiện tại:

Lưu ý cách cuộc gọi đầu tiên đến

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

34 ở trên liệt kê một số tên được tự động xác định và đã có trong không gian tên khi trình thông dịch bắt đầu. Vì các tên mới được xác định (
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

37,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

38,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

39), chúng xuất hiện trên các lời mời tiếp theo của
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

34.

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
3

Khi đưa ra một đối số là tên của một mô -đun,

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

34 liệt kê các tên được xác định trong mô -đun:

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
4

>>>

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
5

Thực hiện một mô -đun dưới dạng tập lệnh

Bất kỳ tệp

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
7 nào chứa một mô -đun về cơ bản cũng là một tập lệnh Python, và có bất kỳ lý do gì mà nó có thể được thực thi như một.module is essentially also a Python script, and there isn’t any reason it can’t be executed like one.

Ở đây một lần nữa là

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8 như đã được xác định ở trên:

mod.py

s = "If Comrade Napoleon says it, it must be right."
a = [100, 200, 300]

def foo(arg):
    print(f'arg = {arg}')

class Foo:
    pass

Điều này có thể được chạy dưới dạng tập lệnh:

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
7

Không có lỗi, vì vậy nó rõ ràng đã làm việc. Cấp, nó không thú vị lắm. Như nó được viết, nó chỉ xác định các đối tượng. Nó không làm bất cứ điều gì với họ, và nó không tạo ra bất kỳ đầu ra nào.

Hãy để sửa đổi mô -đun Python ở trên để nó tạo ra một số đầu ra khi chạy dưới dạng tập lệnh:

mod.py

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
8

Bây giờ nó sẽ thú vị hơn một chút:

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
9

Thật không may, bây giờ nó cũng tạo ra đầu ra khi được nhập dưới dạng mô -đun:

>>>

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
0

Đây có lẽ không phải là những gì bạn muốn. Nó không phải là thông thường cho một mô -đun để tạo ra đầu ra khi được nhập.

Sẽ rất tuyệt nếu bạn có thể phân biệt giữa khi tệp được tải dưới dạng mô -đun và khi nó được chạy như một tập lệnh độc lập?

Hỏi và ngươi sẽ nhận được.

Khi tệp

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
7 được nhập dưới dạng mô -đun, Python đặt biến Dunder đặc biệt
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

45 thành tên của mô -đun. Tuy nhiên, nếu một tệp được chạy dưới dạng tập lệnh độc lập,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

45 được đặt thành chuỗi
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

47. Sử dụng thực tế này, bạn có thể nhận ra trường hợp đó là trường hợp chạy và thay đổi hành vi tương ứng:dunder variable
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

45 to the name of the module. However, if a file is run as a standalone script,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

45 is (creatively) set to the string
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

47. Using this fact, you can discern which is the case at run-time and alter behavior accordingly:

mod.py

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
1

Bây giờ, nếu bạn chạy như một tập lệnh, bạn sẽ nhận được đầu ra:

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
2

Nhưng nếu bạn nhập như một mô -đun, bạn không nên

>>>

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
3

Các mô -đun thường được thiết kế với khả năng chạy như một tập lệnh độc lập cho các mục đích kiểm tra chức năng có trong mô -đun. Điều này được gọi là thử nghiệm đơn vị. Ví dụ: giả sử bạn đã tạo một mô -đun

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

48 chứa chức năng giai thừa, như sau:unit testing. For example, suppose you have created a module
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

48 containing a factorial function, as follows:

fact.py

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
4

Tệp có thể được coi là một mô -đun và hàm

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

49 được nhập:

>>>

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
5

Nhưng nó cũng có thể được chạy như một độc lập bằng cách chuyển một đối số số nguyên trên dòng lệnh để kiểm tra:

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
6

Tải lại một mô -đun

Vì lý do hiệu quả, một mô -đun chỉ được tải một lần cho mỗi phiên phiên dịch. Đó là tốt cho các định nghĩa chức năng và lớp, thường chiếm phần lớn nội dung mô -đun. Nhưng một mô -đun cũng có thể chứa các câu lệnh thực thi, thường là để khởi tạo. Xin lưu ý rằng các câu lệnh này sẽ chỉ được thực thi ngay lần đầu tiên một mô -đun được nhập.

Xem xét tệp sau

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
8:

mod.py

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
7

>>>

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
8

Câu lệnh

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

51 không được thực thi trên các lần nhập tiếp theo. .

Nếu bạn thực hiện thay đổi thành mô -đun và cần tải lại, bạn cần khởi động lại trình thông dịch hoặc sử dụng hàm gọi là

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

53 từ mô -đun
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

54:

>>>

>>> sys.path.append(r'C:\Users\john')
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages', 'C:\\Users\\john']
>>> import mod
9

Gói Python

Giả sử bạn đã phát triển một ứng dụng rất lớn bao gồm nhiều mô -đun. Khi số lượng các mô -đun tăng lên, việc theo dõi tất cả chúng trở nên khó khăn nếu chúng bị đổ vào một địa điểm. Điều này đặc biệt như vậy nếu họ có tên hoặc chức năng tương tự. Bạn có thể mong muốn một phương tiện nhóm và tổ chức chúng.

Các gói cho phép cấu trúc phân cấp của không gian tên mô -đun bằng ký hiệu DOT. Theo cùng một cách mà các mô -đun giúp tránh va chạm giữa các tên biến toàn cầu, các gói giúp tránh va chạm giữa các tên mô -đun. allow for a hierarchical structuring of the module namespace using dot notation. In the same way that modules help avoid collisions between global variable names, packages help avoid collisions between module names.

Tạo một gói khá đơn giản, vì nó sử dụng cấu trúc tệp phân cấp vốn có của hệ điều hành. Xem xét sự sắp xếp sau:package is quite straightforward, since it makes use of the operating system’s inherent hierarchical file structure. Consider the following arrangement:

Hướng dẫn what is the use of modules and packages in python? - việc sử dụng các mô-đun và gói trong python là gì?

Ở đây, có một thư mục có tên

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 chứa hai mô -đun,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

56 và
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

57. Nội dung của các mô -đun là:

mod1.py

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
0

mod2.py

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
1

Với cấu trúc này, nếu thư mục

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 nằm ở một vị trí có thể tìm thấy nó (trong một trong các thư mục có trong
from <module_name> import <name(s)>
0), bạn có thể tham khảo hai mô -đun với ký hiệu DOT (
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

60,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

61) và nhập chúng với cú pháp bạn đã quen thuộc với:modules with dot notation (
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

60,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

61) and import them with the syntax you are already familiar with:

import <module_name>[, <module_name> ...]

>>>

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
3

from <module_name> import <name(s)>

>>>

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
5

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
6

>>>

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
7

Bạn cũng có thể nhập các mô -đun với các câu lệnh này:

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
8

>>>

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
9

Về mặt kỹ thuật, bạn cũng có thể nhập gói:

>>>

>>> import mod
>>> mod

0

Nhưng điều này ít có kết quả. Mặc dù điều này là, nói đúng, một tuyên bố Python chính xác về mặt cú pháp, nhưng nó không làm nhiều điều hữu ích. Cụ thể, nó không đặt bất kỳ mô -đun nào trong

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 vào không gian tên cục bộ:

>>>

>>> import mod
>>> mod

1

Để thực sự nhập các mô -đun hoặc nội dung của chúng, bạn cần sử dụng một trong các biểu mẫu được hiển thị ở trên.

Khởi tạo gói

Nếu một tệp có tên

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

63 có trong thư mục gói, nó sẽ được gọi khi gói hoặc mô -đun trong gói được nhập. Điều này có thể được sử dụng để thực hiện mã khởi tạo gói, chẳng hạn như khởi tạo dữ liệu cấp gói.

Ví dụ: hãy xem xét tệp

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

63 sau:

__init__.py

>>> import mod
>>> mod

2

Hãy để thêm tệp này vào thư mục

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 từ ví dụ trên:

Hướng dẫn what is the use of modules and packages in python? - việc sử dụng các mô-đun và gói trong python là gì?

Bây giờ khi gói được nhập, danh sách toàn cầu

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

66 được khởi tạo:

>>>

>>> import mod
>>> mod

3

Một mô -đun trong gói có thể truy cập biến toàn cầu bằng cách lần lượt nhập nó:module in the package can access the global variable by importing it in turn:

mod1.py

>>> import mod
>>> mod

4

>>>

>>> import mod
>>> mod

5

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

63 cũng có thể được sử dụng để thực hiện nhập khẩu tự động các mô -đun từ một gói. Ví dụ, trước đó, bạn đã thấy rằng câu lệnh
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

68 chỉ đặt tên
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 trong bảng biểu tượng người gọi cục bộ và không nhập bất kỳ mô -đun nào. Nhưng nếu
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

63 trong thư mục
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 chứa các mục sau:

__init__.py

>>> import mod
>>> mod

6

Sau đó, khi bạn thực thi

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

68, các mô -đun
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

73 và
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

74 được nhập tự động:

>>>

>>> import mod
>>> mod

7

Nhập >>> import mod >>> print(mod.s) If Comrade Napoleon says it, it must be right. >>> mod.a [100, 200, 300] >>> mod.foo(['quux', 'corge', 'grault']) arg = ['quux', 'corge', 'grault'] >>> x = mod.Foo() >>> x 75 từ một gói

Đối với các mục đích của cuộc thảo luận sau, gói được xác định trước đó được mở rộng để chứa một số mô -đun bổ sung:

Hướng dẫn what is the use of modules and packages in python? - việc sử dụng các mô-đun và gói trong python là gì?

Hiện có bốn mô -đun được xác định trong thư mục

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55. Nội dung của chúng như được hiển thị dưới đây:

mod1.py

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
0

mod2.py

>>> import mod
>>> mod.__file__
'C:\\Users\\john\\mod.py'

>>> import re
>>> re.__file__
'C:\\Python36\\lib\\re.py'
1

mod3.py

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
0

mod4.py

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
1

(Tưởng tượng, aren họ?)

Bạn đã thấy rằng khi

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 được sử dụng cho một mô -đun, tất cả các đối tượng từ mô -đun được nhập vào bảng ký hiệu cục bộ, ngoại trừ các đối tượng bắt đầu bằng dấu gạch dưới, như mọi khi:module, all objects from the module are imported into the local symbol table, except those whose names begin with an underscore, as always:

>>>

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
2

Tuyên bố tương tự cho một gói là:package is this:

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
3

Cái đó làm cái gì?

>>>

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
4

HMPH. Không nhiều. Bạn có thể đã mong đợi (giả sử bạn có bất kỳ kỳ vọng nào) rằng Python sẽ đi sâu vào thư mục gói, tìm tất cả các mô -đun có thể và nhập tất cả. Nhưng như bạn có thể thấy, theo mặc định đó không phải là những gì xảy ra.

Thay vào đó, Python tuân theo quy ước này: nếu tệp

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

63 trong thư mục gói chứa một danh sách có tên
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79, thì nó được coi là một danh sách các mô -đun nên được nhập khi gặp phải câu lệnh
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

80.package directory contains a list named
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79, it is taken to be a list of modules that should be imported when the statement
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

80 is encountered.

Ví dụ hiện tại, giả sử bạn tạo một

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

63 trong thư mục
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55 như thế này:

pkg/__init__.py

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
5

Bây giờ

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

83 nhập tất cả bốn mô -đun:

>>>

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
6

Sử dụng

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 vẫn không được coi là hình thức tuyệt vời, bất kỳ gói nào hơn cho các gói hơn cho các mô -đun. Nhưng cơ sở này ít nhất cung cấp cho người tạo gói một số điều khiển đối với những gì xảy ra khi
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 được chỉ định. .packages than for modules. But this facility at least gives the creator of the package some control over what happens when
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 is specified. (In fact, it provides the capability to disallow it entirely, simply by declining to define
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79 at all. As you have seen, the default behavior for packages is to import nothing.)

Nhân tiện,

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79 cũng có thể được xác định trong một mô -đun và phục vụ cùng một mục đích: để kiểm soát những gì được nhập với
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30. Ví dụ: Sửa đổi
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

56 như sau:module as well and serves the same purpose: to control what is imported with
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30. For example, modify
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

56 as follows:

pkg/mod1.py

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
7

Bây giờ một câu lệnh

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 từ
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

60 sẽ chỉ nhập những gì có trong
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79:

>>>

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
8

import <module_name>[, <module_name> ...]
2 (hàm) hiện được xác định trong không gian tên cục bộ, nhưng
import <module_name>[, <module_name> ...]
3 (lớp) thì không, vì cái sau không có trong
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79.

Tóm lại,

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

79 được cả hai gói và mô -đun sử dụng để kiểm soát những gì được nhập khi
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 được chỉ định. Nhưng hành vi mặc định khác nhau:packages and modules to control what is imported when
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

30 is specified. But the default behavior differs:

  • Đối với một gói, khi
    >>> import mod
    >>> print(mod.s)
    If Comrade Napoleon says it, it must be right.
    >>> mod.a
    [100, 200, 300]
    >>> mod.foo(['quux', 'corge', 'grault'])
    arg = ['quux', 'corge', 'grault']
    >>> x = mod.Foo()
    >>> x
    
    
    79 không được xác định,
    >>> import mod
    >>> print(mod.s)
    If Comrade Napoleon says it, it must be right.
    >>> mod.a
    [100, 200, 300]
    >>> mod.foo(['quux', 'corge', 'grault'])
    arg = ['quux', 'corge', 'grault']
    >>> x = mod.Foo()
    >>> x
    
    
    30 không nhập bất cứ thứ gì.
  • Đối với một mô -đun, khi
    >>> import mod
    >>> print(mod.s)
    If Comrade Napoleon says it, it must be right.
    >>> mod.a
    [100, 200, 300]
    >>> mod.foo(['quux', 'corge', 'grault'])
    arg = ['quux', 'corge', 'grault']
    >>> x = mod.Foo()
    >>> x
    
    
    79 không được xác định,
    >>> import mod
    >>> print(mod.s)
    If Comrade Napoleon says it, it must be right.
    >>> mod.a
    [100, 200, 300]
    >>> mod.foo(['quux', 'corge', 'grault'])
    arg = ['quux', 'corge', 'grault']
    >>> x = mod.Foo()
    >>> x
    
    
    30 nhập tất cả mọi thứ (ngoại trừ, bạn đoán nó là tên bắt đầu với một dấu gạch dưới).

Đóng gói con

Các gói có thể chứa các gói con lồng nhau đến độ sâu tùy ý. Ví dụ: hãy để Lừa làm thêm một sửa đổi cho thư mục gói ví dụ như sau:subpackages to arbitrary depth. For example, let’s make one more modification to the example package directory as follows:

Hướng dẫn what is the use of modules and packages in python? - việc sử dụng các mô-đun và gói trong python là gì?

Bốn mô -đun (

>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

56,
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

57,
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
04 và
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
05) được định nghĩa như trước đây. Nhưng bây giờ, thay vì được gộp lại với nhau vào thư mục
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

55, chúng được chia thành hai thư mục thanh phần,
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
07 và
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
08.subpackage directories,
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
07 and
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
08.

Nhập khẩu vẫn hoạt động giống như hình trước đây. Cú pháp tương tự nhau, nhưng ký hiệu chấm bổ sung được sử dụng để tách tên gói khỏi tên subpackage:dot notation is used to separate package name from subpackage name:

>>>

>>> s
NameError: name 's' is not defined
>>> foo('quux')
NameError: name 'foo' is not defined
9

Ngoài ra, một mô -đun trong một gói con có thể tham chiếu các đối tượng trong gói con anh chị em (trong trường hợp anh chị em chứa một số chức năng mà bạn cần). Ví dụ: giả sử bạn muốn nhập và thực thi chức năng

import <module_name>[, <module_name> ...]
2 (được xác định trong mô -đun
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

73) từ bên trong mô -đun
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
11. Bạn có thể sử dụng nhập khẩu tuyệt đối:subpackage can reference objects in a sibling subpackage (in the event that the sibling contains some functionality that you need). For example, suppose you want to import and execute function
import <module_name>[, <module_name> ...]
2 (defined in module
>>> import mod
>>> print(mod.s)
If Comrade Napoleon says it, it must be right.
>>> mod.a
[100, 200, 300]
>>> mod.foo(['quux', 'corge', 'grault'])
arg = ['quux', 'corge', 'grault']
>>> x = mod.Foo()
>>> x

73) from within module
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
11. You can either use an absolute import:

pkg/sub__pkg2/mod3.py

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
0

>>>

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
1

Hoặc bạn có thể sử dụng nhập khẩu tương đối, trong đó

>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
12 đề cập đến gói một cấp. Từ trong
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
04, trong thanh toán con
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
08,relative import, where
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
12 refers to the package one level up. From within
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
04, which is in subpackage
>>> import sys
>>> sys.path
['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
08,

  • >>> import sys
    >>> sys.path
    ['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
    'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
    'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
    
    12 đánh giá gói cha mẹ (
    >>> import mod
    >>> print(mod.s)
    If Comrade Napoleon says it, it must be right.
    >>> mod.a
    [100, 200, 300]
    >>> mod.foo(['quux', 'corge', 'grault'])
    arg = ['quux', 'corge', 'grault']
    >>> x = mod.Foo()
    >>> x
    
    
    55) và
  • >>> import sys
    >>> sys.path
    ['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
    'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
    'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
    
    17 Đánh giá cho Subpackage
    >>> import sys
    >>> sys.path
    ['', 'C:\\Users\\john\\Documents\\Python\\doc', 'C:\\Python36\\Lib\\idlelib',
    'C:\\Python36\\python36.zip', 'C:\\Python36\\DLLs', 'C:\\Python36\\lib',
    'C:\\Python36', 'C:\\Python36\\lib\\site-packages']
    
    07 của gói cha.

pkg/sub__pkg2/mod3.py

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
2

>>>

>>> mod.s
'If Comrade Napoleon says it, it must be right.'
>>> mod.foo('quux')
arg = quux
3

Sự kết luận

Trong hướng dẫn này, bạn đã đề cập đến các chủ đề sau:

  • Cách tạo mô -đun Pythonmodule
  • Vị trí nơi thông dịch viên Python tìm kiếm một mô -đun
  • Cách có được quyền truy cập vào các đối tượng được xác định trong một mô -đun với câu lệnh
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    6
  • Cách tạo một mô -đun có thể thực thi như một tập lệnh độc lập
  • Cách sắp xếp các mô -đun thành các gói và đóng gói conpackages and subpackages
  • Cách kiểm soát khởi tạo gói

Điều này hy vọng sẽ cho phép bạn hiểu rõ hơn về cách có được quyền truy cập vào chức năng có sẵn trong nhiều mô-đun của bên thứ ba và tích hợp có sẵn trong Python.

Ngoài ra, nếu bạn đang phát triển ứng dụng của riêng mình, việc tạo các mô -đun và gói của riêng bạn sẽ giúp bạn sắp xếp và mô đun hóa mã của mình, điều này giúp mã hóa, bảo trì và gỡ lỗi dễ dàng hơn.modules and packages will help you organize and modularize your code, which makes coding, maintenance, and debugging easier.

Nếu bạn muốn tìm hiểu thêm, hãy xem tài liệu sau đây tại python.org:Python.org:

  • Hệ thống
    >>> mod.s
    'If Comrade Napoleon says it, it must be right.'
    >>> mod.foo('quux')
    arg = quux
    
    6
  • Hướng dẫn Python: Mô -đun

Happy Pythoning!

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: các mô -đun và gói Python: Giới thiệu This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Python Modules and Packages: An Introduction

Việc sử dụng các mô -đun trong Python là gì?

Mô -đun Python là một tệp chứa các định nghĩa và câu lệnh Python.Một mô -đun có thể xác định các chức năng, lớp và biến.Một mô -đun cũng có thể bao gồm mã Runnable.Nhóm mã liên quan thành một mô -đun giúp mã dễ hiểu và sử dụng hơn.define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use.

Ưu điểm của các mô -đun và gói là gì?

Bằng cách sử dụng một hệ thống mô -đun, chúng ta có thể cho phép một bộ gói giới hạn có thể truy cập được cho các ứng dụng bên ngoài.Nó làm cho ứng dụng của chúng tôi nhẹ, vì vậy nó có thể được chạy trên nhiều thiết bị hơn.Vì nó nhẹ, nó cải thiện hiệu suất của một ứng dụng.allow a limited set of packages to be accessible to outside applications. It makes our application lightweight, so it can be run on more number of devices. As it is lightweight, it improves the performance of an application.

Sự khác biệt giữa mô -đun và gói là gì?

Mô -đun là một tệp chứa tập lệnh Python trong thời gian chạy cho mã được chỉ định cho người dùng.Một gói cũng sửa đổi mã được giải thích của người dùng theo cách mà nó dễ dàng hoạt động trong thời gian chạy.