Attempted relative import beyond toplevel package

后端 未结 1 727
深忆病人
深忆病人 2020-12-09 03:29

Here is my folder structure:

Mopy/ # no init.py !
   bash/
     __init__.py
     bash.py # <--- Edit: yep there is such a module too
     bass.py
     bos         


        
相关标签:
1条回答
  • 2020-12-09 03:59

    TLDR: Do

    import bash.bosh
    

    or

    from bash import bosh
    

    Avoid modifying sys.path, as this duplicates modules.


    When you do

    import bosh
    

    it will import the module bosh. This means Mopy/bash is in your sys.path, python finds the file bosh there, and imports it. The module is now globally known by the name bosh. Whether bosh is itself a module or package doesn't matter for this, it only changes whether bosh.py or bosh/__init__.py is used.

    Now, when bosh tries to do

    from .. import bass
    

    this is not a file system operation ("one directory up, file bass") but a module name operation. It means "one package level up, module bass". bosh wasn't imported from its package, but on its own, though. So going up one package is not possible - you end up at the package '', which is not valid.

    Let's look at what happens when you do

    import bash.bosh
    

    instead. First, the package bash is imported. Then, bosh is imported as a module of that package - it is globally know as bash.bosh, even if you used from bash import bosh.

    When bosh does

    from .. import bass
    

    that one works now: going one level up from bash.bosh gets you to bash. From there, bass is imported as bash.bass.

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