close

[Solved] Python 3: module in same directory as script: “Import: No module named”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python 3: module in same directory as script: “Import: No module named” 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 Python 3: module in same directory as script: “Import: No module named” Error Occurs?

Today I get the following error Python 3: module in same directory as script: “Import: No module named” in python.

How To Solve Python 3: module in same directory as script: “Import: No module named” Error ?

  1. How To Solve Python 3: module in same directory as script: “Import: No module named” Error ?

    To Solve Python 3: module in same directory as script: “Import: No module named” Error Maybe someone will tell you to add init.py under the directory. It won't work for some special cases as well.

  2. Python 3: module in same directory as script: “Import: No module named”

    To Solve Python 3: module in same directory as script: “Import: No module named” Error Maybe someone will tell you to add init.py under the directory. It won't work for some special cases as well.

Solution 1

The makesoup.py file is also located in the processors subdirectory, which means any Python script near it should be able to find it, right?

No. This feature was changed in Python 3 and that syntax no longer works.

Change the import makesoup to this:

from . import makesoup

Or to this:

from processors import makesoup

Both of these will make it impossible to run python processors/venues.py directly, though you can still do python -m processors.venues from your home directory

Solution 2

Sometimes, this does not work:

from . import xxx

Maybe someone will tell you to add init.py under the directory. It won’t work for some special cases as well.

The most useful way would be to check the sys.path first with:

import sys
print(sys.path)

Then you can find where you should import from.

There is an another way as well:

import os
import sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname("__file__"), '..')))

or use insert function instead:

sys.path.insert(0, xxx)

These two ways are suitable for small project. I will recommend you choose the first one if your project is complex and huge.

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