Python: ModuleNotFoundError when trying to import module from imported package

Python: ModuleNotFoundError when trying to import module from imported package

FIRST, if you want to be able to access from AND from, you need to properly setup your files as packages and modules.

Packages are a way of structuring Python’s module namespace by using
“dotted module names”. For example, the module name A.B designates a
submodule named B in a package named A.

When importing the package, Python searches through the directories on
sys.path looking for the package subdirectory.

The files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as string, from unintentionally
hiding valid modules that occur later on the module search path.

You need to set it up to something like this:

|- Mans
|- MansTest
   |- SoftLib
      |- Soft
         |- SoftWork
      |- Unittests

SECOND, for the ModuleNotFoundError: No module named Soft error caused by from ...Mans import man1 in, the documented solution to that is to add to sys.path since Mans is outside the MansTest package. See The Module Search Path from the Python documentation. But if you dont want to modify sys.path directly, you can also modify PYTHONPATH:

sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

THIRD, for from ...MansTest.SoftLib import Soft which you said was to facilitate the aforementioned import statement in, thats now how imports work. If you want to import Soft.SoftLib in, you have to setup to find Soft.SoftLib and import it there directly.

With that said, heres how I got it to work.

from Soft.SoftWork.manModules import *
# no change to import statement but need to add Soft to PYTHONPATH

def foo():
    print(called foo in
    print(foo call module1 from manModules:  + module1())

# no need for from ...MansTest.SoftLib import Soft to facilitate importing..
from ...Mans import man1

def module1():
    return module1 in manModules

Terminal output:

$ python3 -m man.MansTest.Unittests.man1test
Traceback (most recent call last):
    from ...Mans import man1
  File /temp/man/Mans/, line 2, in <module>
    from Soft.SoftWork.manModules import *
ModuleNotFoundError: No module named Soft

$ PYTHONPATH=$PYTHONPATH:/temp/man/MansTest/SoftLib
$ python3 -m man.MansTest.Unittests.man1test
called foo in
foo called module1 from manModules: module1 in manModules 

As a suggestion, maybe re-think the purpose of those SoftLib files. Is it some sort of bridge between and The way your files are setup right now, I dont think its going to work as you expect it to be. Also, its a bit confusing for the code-under-test ( to be importing stuff from under the test folder (MansTest).

I had a similar problem, although not the same.

My folders and files had the structure (GUI_ML is the main folder):

 Views  __ init
 Controllers  __ init

I needed to import from The following code solved my problem:

import sys
import os
myDir = os.getcwd()

from pathlib import Path
path = Path(myDir)


from Controllers.Control_login import Control_login

Python: ModuleNotFoundError when trying to import module from imported package

For me when I created a file and saved it as python file, I was getting this error during importing.
I had to create a filename with the type .py , like and then save it as a python file.
post trying to import the file worked for me.

Leave a Reply

Your email address will not be published. Required fields are marked *