Skip to content Skip to sidebar Skip to footer

Python: How To Import From An __init__.py File?

I'm building a website using the Flask Framework, in which I've got a folder in which I have some python files and an __init__.py script (I guess you would call this folder a modul

Solution 1:

Try relative imports

from . import db

Solution 2:

The init.py files are automatically imported when you import the package they are in. For example, say the init.py is in a package called foo. When you import foo

import foo
from foo import *
import foo as ...

the init file gets called. And are you using python 2 or 3? If you are using python 3, you will need to use dynamic imports:

from * import __init__

In general, it is a bad practice to import from init, I would suggest trying to place this code in another file in the package.

Post a Comment for "Python: How To Import From An __init__.py File?"