Reputation: 2162
I have a python application with the following structure.
/app
/models
__init__.py
profile.py
/views
__init__.py
index.py
login.py
__init.py
main.py
Currently the three __init__.py
files are empty.
In the files like index.py
, I have several classes.
class IndexHandler(webapp2.RequestHandler):
#blah blah
What do I write in the __init.py__
files such that I can call the classes in profile.py
, index.py
and so on without typing the full package path?
Upvotes: 1
Views: 308
Reputation: 12672
Namespaces in Python are explicit. In each __init__.py
file, import whatever modules, functions, and classes you would like to be available within the namespace of that package.
For example, if you would like the app
package to have an index
member, in /app/__init__.py
:
from views import index
Or if you would like the class itself to be part of the namespace:
from models.views import IndexHandler
This will allow you to do:
import app
handler1 = app.index.IndexHandler() # example 1
handler1 = app.IndexHandler() # example 2
Upvotes: 1
Reputation: 6159
You technically don't have to put anything in the __init__.py
file. You should be able to import models from your main.py
file, as long as it is in the app folder, and use the functions like:
import models
models.profile.whatever
If you main.py
is 'above' your /app folder, you will need an __init__.py
file in your /app folder. then you can do:
import app
app.models.profile.whatever
Upvotes: 1