Relative imports - ModuleNotFoundError No module named x

0 votes

I have these  two files:

  1. test.py
  2. config.py

These are the following functions stung to config.py:

config.py

debug = True

test.py

import config
print (config.debug)

But I am getting this error:

ModuleNotFoundError: No module named 'config'

The py3 convention is to use absolute imports:

from . import config

However, this leads to the following error:

ImportError: cannot import name 'config'

 Can someone help me solve this?

May 23, 2022 in Python by Kichu
• 19,040 points
1,919 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The problem here is that you can't do relative imports from the file you execute since __main__ module is not a part of a package. 

Absolute imports - import something available on sys.path.

Relative imports - import something relative to the current module, must be a part of a package.

For you to understand it clearly here's an example:

.

./main.py

./ryan/__init__.py

./ryan/config.py

./ryan/test.py

Then update test.py:

# config.py

debug = True

# test.py

print(__name__)

After that try:

    # Trying to find module in the parent package

    from . import config

    print(config.debug)

    del config

except ImportError:

    print('Relative import failed')

Then try this:

    # Trying to find module on sys.path

    import config

    print(config.debug)

except ModuleNotFoundError:

    print('Absolute import failed')

# main.py

import ryan.test

After doing this try running test.py first:

$ python ryan/test.py

__main__

Relative import failed

True

At this stage import config should work, since the ryan folder will be added to sys.path.

I hope this helps.

answered May 27, 2022 by narikkadan
• 63,600 points

edited Mar 5

Related Questions In Python

+1 vote
3 answers
+2 votes
3 answers

ModuleNotFoundError: No module named 'Crypto'

The module you’ve installed is different. To install ...READ MORE

answered Aug 20, 2019 in Python by Raman
82,949 views
+3 votes
3 answers

ModuleNotFoundError: No module named 'cv2'

Hi@akhtar, This error may occur if you didn't install ...READ MORE

answered Apr 9, 2020 in Python by MD
• 95,460 points
272,273 views
0 votes
2 answers

ModuleNotFoundError: No module named 'pythoncom' in pyttsx3

even i faced the same problem ...just ...READ MORE

answered Jun 2, 2020 in Python by anonymous
14,460 views
+2 votes
2 answers

ModuleNotFoundError: No module named 'pyttsx3

Hi@akhtar, To avoid this error you have to ...READ MORE

answered Apr 24, 2020 in Python by MD
• 95,460 points
30,214 views
0 votes
1 answer

ModuleNotFoundError: No module named 'Foundation'

Hi@akhtar, By default Foundation module comes with Anaconda ...READ MORE

answered Apr 24, 2020 in Python by MD
• 95,460 points
4,275 views
0 votes
1 answer

Relative imports in Python 3

Because the first statement, from.mymodule import myfunction, ...READ MORE

answered Feb 16, 2023 in Python by Rishu
• 300 points
1,334 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 7, 2018 in Python by Priyaj
• 58,020 points
1,797 views
0 votes
1 answer

Crawling after login in Python

You missed a few login data forms, ...READ MORE

answered Sep 14, 2018 in Python by Priyaj
• 58,020 points
936 views
0 votes
1 answer

“stub” __objclass__ in a Python class how to implement it?

You want to avoid interfering with this ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 58,020 points
2,540 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP