Search code examples
python-3.xmodulenotfounderror

Why I have this error: ModuleNotfound error python3


I have the next structure on my project:

structure

I'm trying to run the file tests/test.py that has the next content:

import unittest
from machinetranslation.translator import english_to_french, french_to_english

class TestMain(unittest.TestCase):

    def test_EnglishToFrench(self):
        test_case = "Hello"
        expected = "Bonjour"        
        self.assertEqual(english_to_french(test_case), expected)

    def test_FrenchToEnglish(self):
        test_case = "Bonjour"
        expected = "Hello"        
        self.assertEqual(french_to_english(test_case), expected)

    def test_EnglishToFrench_null(self):
        test_case = None
        expected = "Type a text in English"
        self.assertEqual(english_to_french(test_case), expected)

    def test_FrenchToEnglish_null(self):
        test_case = None
        expected = "Type a text in French"
        self.assertEqual(french_to_english(test_case), expected)


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

But I don't know why I'm getting the error: "ModuleNotFoundError: No module named 'machinetranslation'" Could anyone tell me why this error please, thanks


Solution

  • I have also encountered this issue before, this is what helped me:

    import os, sys
    current_dir = os.path.dirname(os.path.abspath(__file__))
    parent_dir = os.path.dirname(current_dir)
    sys.path.insert(0, parent_dir)
    
    // Import files here