Python: How to import from an __init__.py file?

后端 未结 2 1237
暗喜
暗喜 2020-12-20 15:51

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 ca

相关标签:
2条回答
  • 2020-12-20 16:12

    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.

    0 讨论(0)
  • 2020-12-20 16:17

    Try relative imports

    from . import db
    
    0 讨论(0)
提交回复
热议问题