module has no attribute

后端 未结 4 870
忘了有多久
忘了有多久 2020-11-30 05:41

I have a directory with a number of .py files in it. each file defines some classes. I also have an empty __init__.py in the directory.

Fo

相关标签:
4条回答
  • 2020-11-30 05:44

    You need an __init__.py in the myproject directory too. So your module structure should be:

    myproject
        __init__.py
        mymodule
            __init__.py
            api.py
            models.py
            views.py
    
    0 讨论(0)
  • 2020-11-30 05:52

    The problem is submodules are not automatically imported. You have to explicitly import the api module:

    import myproject.mymodule.api
    print myproject.mymodule.api.MyClass
    

    If you really insist on api being available when importing myproject.mymodule you can put this in myproject/mymodule/__init__.py:

    import myproject.mymodule.api
    

    Then this will work as expected:

    from myproject import mymodule
    
    print mymodule.api.MyClass 
    
    0 讨论(0)
  • 2020-11-30 06:00

    Modules don't work like that.

    from myproject.mymodule import api
    print api.MyClass
    
    0 讨论(0)
  • 2020-11-30 06:10

    If you are an idiot, like me, then also check whether you didn't name your python file the same as the module you are trying to import.

    0 讨论(0)
提交回复
热议问题