1

There is the following structure:

project/
   I-----main.py
   I-----src/
          I----__init__.py (empty)
          I----util.py
          I----shared/
                  I----__init__.py (empty)
                  I----hello.py 


#main.py
from src import util

#util.py (runs without errors)
from shared import hello

#hello.py (runs without errors)
def hello():
    print("Hello")

I try to run main.py, get result

ModuleNotFoundError: No module named 'shared'

How to repair imports in main.py?

2
  • Why are there two __init__.pys? Please check stackoverflow.com/questions/2164258/… Commented May 21, 2021 at 11:53
  • 1
    @newQOpenGLWidget one __init__.py per package, that looks correct to me. __init__.py is not a constructor. Commented May 21, 2021 at 11:55

4 Answers 4

2

There are two main reasons for such kind of failures:

  1. Module import: You are not importing the module correctly. You can either import it using the __init__.py of a module by mentioning the import of its sub-module or you can directly put the full path via an import. For instance, you can use from src.shared import hello. Or, in your __init__.py file, inside the src module, you could add from shared import hello. This will also work because whenever you import a module, the first thing that is being run in that module is the __init__.py of that file.

  2. Circular imports: This is not your case, but I am bringing this up because many times we ignore this and get trapped. A circular import occurs when two modules depend on each other. I have described why it happens and how to avoid it in the post, What is Circular Import and how to avoid it.

Sign up to request clarification or add additional context in comments.

Comments

1

You probably want to from src.shared import hello

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@mufazmi: This one does provide an answer. That said, subsequently contributed answers reiterate this suggestion but with a lot more detail.
it isnt a stable and always working solution
0

i found possible solution (works well):


    #util.py
    import sys
    from pathlib import Path
    
    root = Path(__file__).parent.parent #depends on file's place in general structure
    sys.path.append(str(root))
    import src.shared.hello as hello

Comments

-1

You need to use a relative import:

from .shared import hello

If you run util.py normally, .../project/src is appended to sys.path and Python can find the import. However, when you run main.py, .../project/ is appended to sys.path, and Python can't find the shared module. By using a relative import, Python looks up the module name relative to the current package, and can find the shared module.

See also: Relative imports for the billionth time

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.