Attempted relative import beyond toplevel package Attempted relative import beyond toplevel package python python

Attempted relative import beyond toplevel package


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.


No need to hack or research the importing of the sibling module.Simply go to your project directory and import the module. If project directory is not a package, add init.py to make it a project directory.

# File name ProjectDir/sibling1/main.pyimport ProjectDir.sibling2if __name__=='__main__':    md = sibling2.module()