Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 282
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 1
Kenil Vasani
Asked: December 10, 20202020-12-10T22:47:12+00:00 2020-12-10T22:47:12+00:00In: Python

Relative imports – ModuleNotFoundError: No module named x

  • 1

This is the first time I’ve really sat down and tried python 3, and seem to be failing miserably. I have the following two files:

  1. test.py
  2. config.py

config.py has a few functions defined in it as well as a few variables. I’ve stripped it down to the following:

config.py

debug = True

test.py

import config
print (config.debug)

I also have an __init__.py

However, I’m getting the following error:

ModuleNotFoundError: No module named 'config'

I’m aware that the py3 convention is to use absolute imports:

from . import config

However, this leads to the following error:

ImportError: cannot import name 'config'

So I’m at a loss as to what to do here… Any help is greatly appreciated. 🙂

packagepythonpython-3.xpython-importrelative-import
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-10T22:47:00+00:00Added an answer on December 10, 2020 at 10:47 pm

      TL;DR: 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

      If you’re running both variants in exactly the same way, one of them should work. Here is an example that should help you understand what’s going on. Let’s add another main.py file with the overall directory structure like this:

      .
      ./main.py
      ./ryan/__init__.py
      ./ryan/config.py
      ./ryan/test.py
      

      And let’s update test.py to see what’s going on:

      # config.py
      debug = True
      
      # test.py
      print(__name__)
      
      try:
          # Trying to find module in the parent package
          from . import config
          print(config.debug)
          del config
      except ImportError:
          print('Relative import failed')
      
      try:
          # Trying to find module on sys.path
          import config
          print(config.debug)
      except ModuleNotFoundError:
          print('Absolute import failed')
      
      # main.py
      import ryan.test
      

      Let’s run test.py first:

      $ python ryan/test.py
      __main__
      Relative import failed
      True
      

      Here “test” is the __main__ module and doesn’t know anything about belonging to a package. However import config should work, since the ryan folder will be added to sys.path.

      Let’s run main.py instead:

      $ python main.py
      ryan.test
      True
      Absolute import failed
      

      And here test is inside of the “ryan” package and can perform relative imports. import config fails since implicit relative imports are not allowed in Python 3.

      Hope this helped.

      P.S.: If you’re sticking with Python 3 there is no more need for __init__.py files.

      • 8
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Unable to resolve dependency tree error when installing npm packages

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com