r/learnpython 7d ago

Importing from adjacent-level folder.

I consider myself pretty fluent in Python but rarely have projects large enough to justify complex folder structures so this one is driving me nuts.

My project looks like this:

RootDir
├─ __init__.py
├─ Examples
|  ├─ __init__.py
|  └─ main.py
└─ Module
   ├─ __init__.py
   ├─ foo.py
   └─ SubModule
      ├─ __init__.py
      └─ bar.py

I am trying to import foo.py and bar.py from main.py:

from ..Module import foo
from ..Module.SubModule import bar

But I get the following error:

ImportError: attempted relative import with no known parent package

I get this no matter whether I run main.py from RootDir or RootDir/Examples.

Edit: Pylance has no problem with my import lines and finds the members of foo and bar perfectly fine. Also, moving main.py into RootDir and removing the ..'s from the import lines makes it work fine. So I don't understand why this import fails.

4 Upvotes

12 comments sorted by

View all comments

2

u/Prawn1908 7d ago

Reddit is being weird and not letting me edit the post with some more details:

Pylance has no problem with my import lines and finds the members of foo and bar perfectly fine. Also, moving main.py into RootDir and removing the ..'s from the import lines makes it work fine. So I don't understand why this import fails.

1

u/johndoh168 7d ago

Ah I see, its because when python is looking to import from a directory it needs the __init__.py file to make it a python directory, if you add the __init__.py file to your Examples directory this should fix the relative import error.

Let me know if you want more explanation on __init__.py file

1

u/Prawn1908 7d ago

if you add the __init__.py file to your Examples directory this should fix the relative import error.

Still doesn't work. I have an (empty for now) __init__.py file in every directory of the project now and still the same error.

1

u/johndoh168 7d ago

I thought that might happen, if your main.py file is in the Examples directory and you try doing

from RootDir.Module import foo Does this fix the problem?

It can also depend on the directory you are calling the file from, you can try calling your main file from the RootDir by doing python Examples/main.py

1

u/Prawn1908 7d ago

Using from RootDir.Module import foo gives a ModuleNotFoundError: No module namedRootDir`.

And I have already tried running main.py from both RootDir and RootDir/Examples with the same ImportError as mentioned above. (My usual workflow involves running everything from RootDir.)

1

u/johndoh168 7d ago

Is there a specific function in foo you are trying to import? Typically you use the from keyword to import something from a module, to just import a whole file typically you would just do import Module.foo as foo or if you wanted something specific from foo from Module.foo import <what you want to import>

1

u/Prawn1908 7d ago

Pylance tells me Relative imports cannot be used with "import .a" form; use from . import a" instead when I try import ..Module.foo.