Python Import Statement

Posted on with tags: flask / python

Python import statement seems very easy to use, but it can get complicated once the project scale becomes larger. It is one of those things that is easy to learn by looking at an example. Miguel Grinberg’s book Flask Web Development Chapter 7 has such an example. The flask apps project structure looks like this.

├── app
│   ├──
│   ├──
│   ├── main
│   │   ├──
│   │   ├──
│   │   ├──
│   │   └──
│   ├──
│   ├── static
│   │   └── favicon.ico
│   └── templates
│       ├── 404.html
│       ├── 500.html
│       ├── base.html
│       ├── index.html
│       ├── mail
│       │   ├── new_user.html
│       │   └── new_user.txt
│       └── user.html
├── data.sqlite
├── migrations
├── requirements.txt
└── tests
7 directories, 24 files

When you are running the app (command flask run), you are in the /flasky directory. The app/ file defines a create_app function which is the app factory. It imports config dict from file like this.

from config import config

The file imports the create_app from app/ Here you can think of app/ as an file. The also imports db models from app/

from app import create_app
from app.model import User

The file also imports create_app. It uses the same import statement shown above. You can look at app here similar to an absolution path.

Relative path is a little more difficult. The app/main/ file imports User model from app/ file. The statement looks like this. The first dot(.) goes into app/main directiory, and the second dot goest into app directory.

from ..models import User
# from app.models import User  # could be

The interesting import statement is a circular one. The app/main/ file imports views and errors modules at the end of the file. This import statement causes the code in and to run. The purpose is to register view functions and error handler functions (via decorators).

# app/main/
from flask import Blueprint
main = Blueprint('main', __name__)
from . import views, errors  # MUST be at the end of the file

The app/main/ file also imports main from app/main/ like this.

from . import main