close

[Solved] __init__.py can’t find local modules

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error init.py can’t find local modules in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How init.py can’t find local modules Error Occurs?

Today I get the following error init.py can’t find local modules in python.

How To Solve init.py can’t find local modules Error ?

  1. How To Solve init.py can't find local modules Error ?

    To Solve init.py can't find local modules Error Putting a . before the module name tells Python that the module you're loading is inside the current module's directory.

  2. init.py can't find local modules

    To Solve init.py can't find local modules Error Putting a . before the module name tells Python that the module you're loading is inside the current module's directory.

Solution 1

Put the following codes in the __init__.py inside the Animals directory.

Python 3.x :

from .Mammals import Mammals
from .Birds import Birds

On 2.x:

from __future__ import absolute_import
from .Mammals import Mammals
from .Birds import Birds

Explanation:

It can’t find the module because it doesn’t know what directory to search to find the files Mammals and Birds. You’re assuming that the subfolder Animals gets added to the python search path, but if you check sys.path (executed from Projects/Animals/__init__.py) you’ll see that only the path to Project is on the path. I’m not sure why the directory containing Project/Animals/__init__.py is not searched, since that’s the code being executed, but the error indicates this is the cause.

Putting a . before the module name tells Python that the module you’re loading is inside the current module’s directory.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read