ImportError : Attempted relative import with no known parent package

前端 未结 4 2052
你的背包
你的背包 2020-12-07 00:56

I am learning to program with python and I am having issues with importing from a module in a package. I have tested commenting off the jedi enabled part and it is not worki

相关标签:
4条回答
  • 2020-12-07 01:00

    Try this...

    from ecommerce.database import Database
    
    0 讨论(0)
  • 2020-12-07 01:12

    Since you are using Python 3.8 version, the imports work a little differently, but I think this should work:

    Use either:

    from database import Database
    #Database is the class
    

    or try:

    import database.Database
    

    lastly, this one is very secure and best practice possibly:

    from . import Database  
    # The '.' (dot) means from within the same directory as this __init__.py module grab the Database class.
    
    0 讨论(0)
  • 2020-12-07 01:21

    This may seem like a hack but it actually work, this happens because of bad package structure

    In you case try importing

    from .vs_code.ecommerce import database

    Now wherever you want to call the class constructor do this:

    database.Database()

    you can assign this to a variable and use it.

    0 讨论(0)
  • 2020-12-07 01:24

    try the following

    from ecommerce import database
    
    0 讨论(0)
提交回复
热议问题