I am making a flask web app and I have something I don't understand.
My app module folder structure goes like this :
app\
| static\
| templates\
| tmp\
| __init__.py
| run.py <= debug script
| toolxls.py <= helper functions
| views.py
in my init.py :
from flask import Flask
app = Flask(__name__)
from app import views
now if I import app module from IDLE:
>>> import app
>>> dir(app)
['Flask', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'app', 'views']
module app has no toolxls sub-module. How can I add toolxls.py into app?
asked Jan 28, 2013 at 13:53
thkang
11.6k15 gold badges71 silver badges90 bronze badges
1 Answer 1
In Python, submodules are not imported when you import the package. You must import them explicitly if you want access to their namespace.
import app.toolxls
answered Jan 28, 2013 at 13:58
Ignacio Vazquez-Abrams
804k160 gold badges1.4k silver badges1.4k bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
thkang
after
from app import toolxls I can access app.toolxls. My question : How can I access toolxls from app in app's __init__.py?thkang
I meant that after adding
from app import toolxls to __init__.py I can use toolxls from elsewhere via app.toolxls. the behavior I don't understand is, app isn't initialized before __init__.py but __init__.py can access app and import toolxls. How is this possible?Ignacio Vazquez-Abrams
Python doesn't require initialization to be complete.
lang-py
from . import views?