2

I have the following structure:

  • project/source_code/constants.py
  • project/source_code/calculator.py
  • project/tests.py
# In 'constants.py'

MIN_VALUE = 0
MAX_VALUE = 100
# In 'calculator.py'

import constants

class Calculator:
    #Create initialiser (not used at the moment)
    def __init__(self):
        return

    def get_sum(self, number1, number2):
        return number1 + number2

def main():
    calculator = Calculator()
    print(calculator.get_sum(10, 10))
    return

if __name__ == "__main__":
    main()
    print("")
    exit(0)
# In 'tests.py'

import unittest
import source_code.calculator as Calculator

class Test_Calculator(unittest.TestCase):
    def setUp(self):
        self.calculator = Calculator()
        return

    def test_calculator(self, number1, number2, errorMessage):
        self.assertEqual(self.get_sum(number1, number2), expected_LBTT, errorMessage)
        return

    def main(self):
        test_calculator(10, 10, "The calculated sum is wrong.")
        return

if __name__ == "__main__":
    unittest.main()

When I run calculator.py, it works fine.

When I run either tests.py or python -m unittest, I get the following error:

> ======================================================================
> **ERROR: tests (unittest.loader._FailedTest.tests)
> **----------------------------------------------------------------------
> ImportError: Failed to import test module: tests
> Traceback (most recent call last):
> File "C:\Users\Home\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py", line 407, in _find_test_path
> module = self._get_module_from_name(name)
>              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> File "C:\Users\Home\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py", line 350, in _get_module_from_name
> __import__(name)
> File "C:\Users\Home\Desktop\BJSS\project\tests.py", line 2, in <module>
> import source_code.calculator as Calculator
> File "C:\Users\Home\Desktop\BJSS\project\source_code\calculator.py", line 1, in <module>
> import constants
> ModuleNotFoundError: No module named 'constants'
>
>
> ----------------------------------------------------------------------
> Ran 1 test in 0.000s
>
> FAILED (errors=1)

I don't understand why it runs one way, but not the other. How can I fix this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David
  • 21
  • 2
  • Hey David! When you import modules from different directories, the directory that contains the modules you want to import needs to have a `__init__.py` file (which in this case would be empty). Try googling: how to import py files/modules in different folders/directories. – Übermensch Mar 27 '23 at 19:27
  • https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python – Übermensch Mar 27 '23 at 19:32
  • Hi Übermensch! Thanks for your answer. I have tried adding __init__.py to the directory as you suggested. However, I still get the same error. – David Mar 27 '23 at 19:57
  • If the file is called `init.py`, you need to add the double underscores so that its `__init__.py`. If you are using a virtual environment then it complicates this a bit. You would need to add the original source_code directory or a copy of it to `venv_name\Lib\site-packages\`. – Übermensch Mar 27 '23 at 20:06
  • Yes, that's what I added. It removes the underscores in my comment. If change "import constants" to "from source_code import constants" then it works when I run the unittest but not when I run the lbtt_calculator.py – David Mar 27 '23 at 20:33
  • 1
    Does this answer your question? [How to do relative imports in Python?](https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python) – Daniil Fajnberg Mar 28 '23 at 08:46
  • The directory you launch your program from is making the difference for the `import constants` in `calculator.py`. You probably want it to be `import source_code.calculator as calculator`. The unittest can't be launched from the source_code directory itself like calculator.py can. – Andrew Allaire Mar 28 '23 at 14:42

0 Answers0