0

I have a file system like this at the moment.

app
 |--__init__.py (empty)
 |    
 |--domain
 |    |--__init__.py (empty)
 |    |--model.py
 |    |--questionmatcher.py
 |
 |--interface
 |    |--__init__.py (empty)
 |    |--basiccli.py
 |    |--userinterface.py
 |
 |--parser
 |    |--__init__.py (empty)
 |    |--json_loader.py
 |    |--parsing.py
 |
 |--testfiles
 |    |--__init__.py (empty)
 |    |--testsuite.py

I am trying to run the testsuite.py which will need to import classes from various files in directory.

I have tried this structure:

import unittest
from ..parser.json_loader import JsonLoader
from ..parser.parsing import get_vectors, parseThreadsFromFile, getPostsFromThreads
from ..domain import UniversalEncoder, SentBERT

class TestParsing(unittest.TestCase):
    def test(self):
        pass


class TestJson(unittest.TestCase):
    def test(self):
        pass


class TestModelEncoders(unittest.TestCase):
    def test(self):
        pass



if __name__ == "__main__":

    unittest.main()

However when I go to run the test I get:

    from ..parser.json_loader import JsonLoader
ImportError: attempted relative import with no known parent package

EDIT:

I have tried

from parser.json_loader import JsonLoader

but now I get

    from parser.json_loader import JsonLoader
ModuleNotFoundError: No module named 'parser.json_loader'; 'parser' is not a package

1 Answers1

0

you can add this package to your PYTHONPATH environmental variable:

export PYTHONPATH=$PYTHONPATH:/path/to/parser
astromonkey
  • 443
  • 2
  • 5
  • 11
  • I'd rather not go adding things like that. It shouldn't be this hard to import packages. But it seems like reading a view of the other similar stackoverflow questions, people are having big troubles with understanding relative imports – Jeff Jefferson Aug 17 '21 at 09:04