Does __init__.py have to be in every directory of python application? Does __init__.py have to be in every directory of python application? flask flask

Does __init__.py have to be in every directory of python application?


Solution was to this problem.1: First there are two things to understand there is a difference in root directory of virtualenv and your system environment. Virtualenv sets your project dir to it's root.But in case you are not using virtualenv the root would be your system directory in my experience. So while creating __init__py keep in mind it can be empty if there is no virtualenv or you haven't imported anything.

simple __init_.py examples:

# in your __init__.pyfrom file import File# now import File from packagefrom package import File

My init.py

import src from src import *from .models.user import Userfrom .models.post import Postfrom .models.blog import Blog

And when you import in app.py or src/models/user.py which is in a same directory & subdirectory you will not include src:

from models.user import User from common.database import Database


You didn't import src first.

import src__all__ = ['models', 'common']from src.models.user import Userfrom src.models.post import Postfrom src.models.blog import Blog

If this is src/__init__.py, you can use a relative import:

from __future__ import absolute_importfrom .models.user import Userfrom .models.post import Postfrom .models.blog import Blog