0

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
1
  • Have you tried from . import views? Commented Jan 28, 2013 at 13:57

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

after from app import toolxls I can access app.toolxls. My question : How can I access toolxls from app in app's __init__.py?
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?
Python doesn't require initialization to be complete.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.