0

I want to run unit tests using unittest module, this is the file organization of my project:

frac
├── frac.py
├── __init__.py
└── test
    └── run_tests.py

The code inside run_tests.py is:

import unittest
from frac import Fraction

class RunFractionTests(unittest.TestCase):
    def test_dunder_mul(self):
        self.assertEqual(Fraction(1, 2) * Fraction(1, 2), Fraction(1, 4))


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

The problem is, when i run run_tests.py with python run_tests.py, i get an error saying that the module frac was not found, this is the error message:

Traceback (most recent call last):
  File "/home/maurothecreator/Área de Trabalho/Projects/HighMath/frac/test/run_tests.py", line 2, in <module>
    from frac import Fraction
ModuleNotFoundError: No module named 'frac'

Am i importing this in the wrong way, if so, how can i fix this?

  • is the top `frac` directory on your pythonpath? How do you run `frac.py` standalone? by moving to the directory it's in? Python puts the current directory in the pythonpath, so "things can work" even if you don't have your pythonpath set correctly to use python files from other locations. like `frac/test`. – JL Peyret Apr 01 '21 at 14:36
  • @JLPeyret i just run `python frac/test/run_tests.py` and then i get this error, i've used `echo $PYTHONPATH` and the return value is just an empty string –  Apr 01 '21 at 14:49
  • well, there you have it. You need to set up your pythonpath correctly. oh, and if you use the terminal using spaces in <Área de Trabalho> is going to bite you, a lot. – JL Peyret Apr 01 '21 at 17:48
  • one way to set up python path is to make your workarea into an installable local pip, using the [`-e` pip flag](https://pip.pypa.io/en/stable/reference/pip_install/#install-editable) . Or you can use a .pth file. or follow theeks suggestion, but do it in the appropriate .bashrc-type file. the easiest, to me, is the [.pth](https://stackoverflow.com/questions/15208615/using-pth-files). – JL Peyret Apr 01 '21 at 17:53

1 Answers1

0

Try adding the following in the console before running python run_tests.py

export PYTHONPATH="${PYTHONPATH}:<path_to_frac>"
theekshanawj
  • 485
  • 5
  • 9