Home > python > Relative import in Python

Relative import in Python

The following post is based on nosklo’s answer who explained relative imports very nicely in this thread.

Imagine the following project structure:

app/ ->
    package_a/ ->
    package_b/ ->

Problem: how to use module_b.py from module_a.py?


  1. you run: python main.py
  2. main.py does: import app.package_a.module_a
  3. module_a.py does: import app.package_b.module_b

Alternatively, 2 or 3 could use: from app.package_a import module_a

That will work as long as you have app in your PYTHONPATH. main.py could be anywhere then.”

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: