2

I have a project which has a folder structure like so:

project:
    folder1:
        __init__.py
        file11.py
    folder2:
        __init__.py
        file21.py
    folder3:
        __init__.py
        file31.py
    __init__.py
    file1.py
    file2.py
    ..

From the root of the project, I am able to import the files on same path

Example,

In file1.py, from file2 import 123 and from folder3.file31 import 456 works

In file21.py, import file1 works

My issue is triggered when I do this:

Example,

In file21.py, from folder3.file31 import xyz

This is throwing an error like ImportError: No module named folder3

All my __init__.py files are empty and make python believe they are packages. I do not understand why this is happening. I would like to know a solution for this and also some little insights on how it works.

Varun Verma
  • 213
  • 3
  • 12

2 Answers2

1

Module file31.py is inside package project.folder3 and not inside just project, nor the same package as module file21.py. Try using in file21.py:

from folder3.file31 import xyz

Instead of

from file31 import xyz
Hyster
  • 646
  • 5
  • 15
1

You should write

from folder3.file31 import xyz
puf
  • 359
  • 3
  • 13