"ModuleNotFoundError: No module named ..." error when running a unittest that is in tests folder in python

Viewed 660

I have the following file/folder structure:

testpackage (folder)  
     src (folder)
         __init__.py  
         module1.py 
     tests
         __init__.py  
         test_module1.py  

just for clearance: the "module1.py" is under the "src" folder which is under the "testpakcage" folder. "tests" is also under the "testpakcage" folder - same level as the "src" one.

module1.py has a class named "class1" as so:

class class1:
    def method1 (self):
        print('i am method1')

in test_module1.py I want to run tests on the above module. this is it's contents:

import unittest

from testpackage.src import module1

t = module1.class1()
t.method1()

this package is not installed, and I don't instead to install or submit it anywereh, I'm just trying to find the best structuring practice for me, for future packaging creation.

problem is: when I run the following either from the "tests" or "testpackage" folder:

/usr/bin/python3.6 -m unittest discover

I get the following error:

E
======================================================================
ERROR: test_module1 (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_module1
Traceback (most recent call last):
  File "/usr/lib64/python3.6/unittest/loader.py", line 428, in _find_test_path
    module = self._get_module_from_name(name)
  File "/usr/lib64/python3.6/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)

something similar also happens when I just try to run "test_module1.py" from the "tests" folder:

Traceback (most recent call last):
  File "test_module1.py", line 5, in <module>
    from testpackage.src import *
ModuleNotFoundError: No module named 'testpackage'

so I tried changing the "import" line with a few alternatives but none of them work. each one of those was a different attempt (not all of them at once):

from testpackage.src import *
import testpackage.src.module1 as module1
import ..src.module1
from ..src.module1 import class1

searching stackoverflow I found solutions that worked for some but not for those using python 3 and above - which is my case.

any suggestions? I think what I'm trying to do is rather simple and I'm missing something really basic here.

I'm using python3.6 by way

1 Answers

I'm wondering if you saw different errors for some of those different import attempts you made. Not sure if this will solve your problem, but this is one way it would generally be accomplished.

First, I am not sure what the top level of your package is supposed to be. Do you reference testpackage in any of the code in the src folder? If so, that top folder should also contain an __init__.py file.

Now, at the very top of your test_module1.py file, add:

import sys
pkg_dir = ".." # if "src" if the top level folder else "..." if testpackage is the top level folder
sys.path.append(pkg_dir)

Note you must change pkg_dir depending on your module's structure.(which I cannot tell from your question).

What this code does is add the folder containing the top level folder of your package to the python import search tree. This can solve problems that the relative import in your example file will not: if the files in your module use module-level imports (e.g. import testpackage.src.module2 in module1.py). This is common in packages with multiple submodules that cross-import.

Related