Python conditional 'module object has no attribute' error with personal package distinct from circular import issue

前端 未结 2 1136
[愿得一人]
[愿得一人] 2021-02-19 11:27

I\'m getting a \'module object has no attribute ...\" error when trying to use a package heirarchy I created. The error is reminiscant of the error you get when there is a circ

2条回答
  •  眼角桃花
    2021-02-19 11:39

    Instead of using absolute imports, it might help to use relatives.

    i.e.

    alpha/bravo/_init_.py

    import alpha.bravo.charlie
    

    should be

    import charlie
    

    Otherwise, it probably is a circular import. i.e. if you import alpha.bravo.charlie from charlie, that means

    alpha/__init__.py
    bravo/__init__.py
    charlie/__init__.py 
    

    All are loaded (or rather, prevented from doing so since they're already loaded). That might cause the problem you're seeing.

提交回复
热议问题