1. __init__.py用来标识所在目录是一个python的模块包(module package)
request
|__init__.py --> print('I from the __init__.py of request.')
|api.py --> def view(): pass
>>> import request
I from the __init__.py of request.
2. __init__.py用来控制模块的导入,对外提供功能
# __init__.py 导入其他功能时,注意工作目录
from request.api import view
print('I from the __init__.py of request.')
# 自己开发的项目,如request的启动目录是固定的,所以from request.api import view
# 作为开源的reuqests使用相对导入较好 .就表示__init__.py所在的目录及requests
from .__version__ import __title__, __description__, __url__, __version__
from .__version__ import __build__, __author__, __author_email__, __license__
from .__version__ import __copyright__, __cake__
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
)
# reqeusts 使用方式
>>> requests.__version__
>>> requests.PreparedRequest()
>>> requests.get(url='http://www.baidu.com')
>>> requests.Session()
>>> from requests import codes
>>> from requests import ConnectTimeout
>>> chardet.detect(b'Hello, world!')
{'encoding': 'ascii', 'confidence': 1.0, 'language': ''}
>>> chardet.detect('中华人民共和国'.encode('gbk'))
{'confidence': 0.99, 'encoding': 'GB2312', 'language': 'Chinese'}
# 几乎所有的三方模块都会有相关代码段,后续会有logging源码分析
import logging
from logging import NullHandler
logging.getLogger(__name__).addHandler(NullHandler())