Why not being able to call modules from lib folder into test folder although I have __init__.py file both?

Arup Rakshit ar at zeit.io
Thu Apr 25 03:44:45 EDT 2019

I am not able to call modules from lib folder in my test folder, but outside of tests folder I can access them. How should I fix this?

Mocks$ tree .
├── lib
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-37.pyc
│   │   └── product.cpython-37.pyc
│   └── product.py
└── tests
    ├── __init__.py
    └── product_test.py

3 directories, 6 files
Mocks$ python3
Python 3.7.3 (default, Mar 27 2019, 09:23:15) 
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from lib.product import ProductionKlass
>>> ProductionKlass()
<lib.product.ProductionKlass object at 0x10d5f2128>
>>> exit()
Mocks$ python3 tests/product_test.py 
Traceback (most recent call last):
  File "tests/product_test.py", line 4, in <module>
    from lib.product import ProductionKlass
ModuleNotFoundError: No module named 'lib'

The file product_test.py has content like:

from unittest import mock
import unittest

from lib.product import ProductionKlass

class TesProductKlass(unittest.TestCase):
    def setUp(self):
        self.real = ProductionKlass()
        self.real.something = mock.MagicMock()
    def test_method(self):
        self.real.something.assert_called_once_with(1, 2, 3)

if __file__ == "__main__":


Arup Rakshit
ar at zeit.io

More information about the Python-list mailing list