在 目录下创建__init__.py文件,然后在里面可以用import和from……import的方式导入其它包的模块或类,这样就可以用创建好的包直接导入其它已经在__init__.py导入的模块。
以下是示例:
flask包中的__init__.py文件内容如下:
__version__ = '0.12' # utilities we import from Werkzeug and Jinja2 that are unused # in the module but are exported as public interface. from werkzeug.exceptions import abort from werkzeug.utils import redirect from jinja2 import Markup, escape from .app import Flask, Request, Response from .config import Config from .helpers import url_for, flash, send_file, send_from_directory, \ get_flashed_messages, get_template_attribute, make_response, safe_join, \ stream_with_context from .globals import current_app, g, request, session, _request_ctx_stack, \ _app_ctx_stack from .ctx import has_request_context, has_app_context, \ after_this_request, copy_current_request_context from .blueprints import Blueprint from .templating import render_template, render_template_string # the signals from .signals import signals_available, template_rendered, request_started, \ request_finished, got_request_exception, request_tearing_down, \ appcontext_tearing_down, appcontext_pushed, \ appcontext_popped, message_flashed, before_render_template # We're not exposing the actual json module but a convenient wrapper around # it. from . import json # This was the only thing that Flask used to export at one point and it had # a more generic name. jsonify = json.jsonify # backwards compat, goes away in 1.0 from .sessions import SecureCookieSession as Session json_available = True
这样,当你在其它文件import flask 时,就可以用from flask import * 的方式导入所有在__init__.py导入的一切,如导入werkzurg.utils 的redirect,就可以写成
from flask import redirect