Simon
Simon

Reputation: 10160

How to import from sibling module in a package?

My package has the following structure:

myPackage
 -- __init__.py <-- empty
 -- signal.py
 -- plot.py

signal.py contains:

from plot import plot_signal

def build_filter():
   ...

def other_function():
   plot_signal()

plot.py contains:

from signal import build_filter

def plot_signal():
   ...

def other_function():
   build_filter()

I then have my script that makes use of this package that contains something like:

import myPackage as mp

mp.plot.plot_signal()

When I run this I get an attribute error: module 'myPackage' has no attribute 'plot'

I'm not sure why its referring to plot as an attribute when its a module in my package, or why its referring to myPackage as a module.

I then tried importing my package and calling the function a different way:

from myPackage import plot, signal

plot.plot_signal()

However, now I get an import error: cannot import name 'build_filter' and the traceback is referring to plot.py where its trying to import the build_filter() function from a sibling module.

I'm thinking this has something to do with the fact that the 2 modules are using functions from one another, and recursively importing the other module.

What's the correct way to organize this package so that sibling modules can import functions from each other?

Upvotes: 6

Views: 2065

Answers (1)

matino
matino

Reputation: 17725

To make mp.plot.plot_signal() work, you must import plot in the myPackage.__init__.py. Another thing is that in both plot.py and signal.py you should import the whole module to avoid circular imports:

signal.py:

import myPackage.plot

myPackage.plot.plot_signal()

plot.py

import myPackage.signal

myPackage.signal.build_filter()

You could also use the relative imports in all 3 files but it will work only in Python 3.x:

signal.py:

from . import plot

plot.plot_signal()

plot.py

from . import signal

signal.build_filter()

Upvotes: 4

Related Questions