Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (47.6k points)

Imagine this directory structure:




I'm coding mod1, and I need to import something from mod2. How should I do it?

I tried from ..sub2 import mod2 but I'm getting an "Attempted relative import in non-package".

I googled around but found only "sys.path manipulation" hacks. Isn't there a clean way?

1 Answer

0 votes
by (106k points)

The main problem is that you're running the module as '__main__' by passing the as an argument to the interpreter.

So you can use the following piece of code to overcome your problem:-

app/ ->

   package_a/ ->

  package_b/ ->

Explanation of the code:-

You will run python so this  imports app.package_a.module_a and the file imports app.package_b.module_b.

Related questions

0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94.1k users

Browse Categories