我們先從package
開始吧。根據python文件的說法,package是一種帶有__path__屬性的module
A Python module which can contain submodules or recursively, subpackages.
Technically, a package is a Python module with an __path__ attribute.
根據這段描述帶出了兩個問題
module
__path__
先來看第一個問題,module是甚麼概念呢?
An object that serves as an organizational unit of Python code.
Modules have a namespace containing arbitrary Python objects.
Modules are loaded into Python by the process of importing.
namespace代表甚麼呢?
local
, global
and built-in
namespaces as well as nested namespaces
in objects (in methods)再來看第二個問題
A package’s __path__ attribute is used during imports of its subpackages.
Within the import machinery, it functions much the same as sys.path, i.e. providing a list of locations to search for modules during import.
__path__
的目的是存放import過程中會使用到的資料,與sys.path的功能性上類似結論是一個包含__path__
屬性的物件即為package
有兩種類型的package
因為requests目錄帶有__init__.py,所以可以將requests看成是一個regular package,當requests被其他module import時,預設會執行__init__.py
from . import utils
from . import packages
from .models import Request, Response, PreparedRequest
from .api import request, get, head, post, patch, put, delete, options
from .sessions import session, Session
from .status_codes import codes
from .exceptions import (
RequestException, Timeout, URLRequired,
TooManyRedirects, HTTPError, ConnectionError,
FileModeWarning, ConnectTimeout, ReadTimeout
)