yamini goel
yamini goel

Reputation: 539

ModuleNotFoundError while importing from different folder (Python)

I am having the age old problem of Module not found while importing files from different folder, kindly help me. My project directory has the following things:

knowledge_generators --> __init__.py
                        knowledge_generator.py
absorb.py

In __init__.py I have the following content:

from knowledge_generator import *

And absorb.py has:

from knowledge_generators import *

On running absorb.py I get the following error:

  File "D:/some/path/project/absorb.py", line 2, in <module>
    from knowledge_generators import *

  File "D:\some\path\project\knowledge_generators\__init__.py", line 1, in <module>
    from knowledge_generator import *

ModuleNotFoundError: No module named 'knowledge_generator'

Also, on running __init__.py everything's working fine(i.e no ModuleNotFoundError). Kindly help me decipher the problem.

Upvotes: 3

Views: 7956

Answers (2)

Bharti Jha
Bharti Jha

Reputation: 191

Working solution: Just add your project root directory to environment variable: PYTHONPATH. so for the below project structure, just add Rootdir path(For e.g: add E:\Projects\Rootdir) in PYTHONPATH.

Rootdir
└── pkg2
    ├── b.py
    ├── c.py
     └── pkg2
      ├── b.py
      ├── c.py
    

Upvotes: 0

Phydeaux
Phydeaux

Reputation: 2855

I suspect you need to use a relative import:

In __init__.py:

from .knowledge_generator import *

Upvotes: 0

Related Questions