How to import python class file from same directory?

前端 未结 2 1647
一生所求
一生所求 2021-02-01 02:41

I have a directory in my Python 3.3 project called /models.

from my main.py I simply do a

from models import *

in my __

相关标签:
2条回答
  • 2021-02-01 03:07

    Since you are using Python 3, which disallows these relative imports (it can lead to confusion between modules of the same name in different packages).

    Use either:

    from models import finding
    

    or

    import models.finding
    

    or, probably best:

    from . import finding  # The . means "from the same directory as this module"
    
    0 讨论(0)
  • 2021-02-01 03:27

    Apparently I can do: from .finding import Finding and this works.

    And the answer below reflects this as well so I guess this is reasonably correct.

    I've fixed up my file naming and moved my tests to a different directory and I am running smoothly now. Thanks!

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