3

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.

1
  • which version of Python are you using? Commented Feb 15, 2018 at 14:26

2 Answers 2

0

I suspect you need to use a relative import:

In __init__.py:

from .knowledge_generator import *
Sign up to request clarification or add additional context in comments.

Comments

0

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
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.