I'm creating a project with the following folder structure.
A project with the 'src' package inside it. The 'models' package and the 'tests' package are inside 'src' and each has its own modules inside.
The problem: I'm trying import the package 'models' in a module inside the 'tests' package.
This is the folder structure:
-project
-src
-models
-__init__.py
-ArmModel.py
-tests
-test_arm_model.py
The ArmModel.py defines a class:
class ArmModel:
def __init__(self, definition='std def'):
self.definition = definition
And the test_arm_model.py (where I have the problem) needs to import ArmModel from models to test it:
import unittest
from src import models
class TestArmModel(unittest.TestCase):
def test_arm_model(self):
arm = models.ArmModel('arm')
self.assertEqual(arm.definition, 'arm')
if __name__ == '__main__':
unittest.main()
I'm having trouble importing "Arm.py" from "models" inside the "tests" module.
Traceback (most recent call last): File "./src/tests/test_arm_model.py", line 2, in from src import models ModuleNotFoundError: No module named 'src'
Attempt 1: I tried to use the following code and got the same error
from src import models
I tried to get the class directly as following and got the same error
from src.models.ArmModel import ArmModel
And I tried to manipulate the sys.path,
import sys
sys.path.append('/home/jamal/dev/tcc/src/')
from src.models.ArmModel import ArmModel
And got the same error
from src.models.ArmModel import ArmModel
ModuleNotFoundError: No module named 'src'
When I tried
sys.path.append('/home/jamal/dev/tcc/src/models/')
or
sys.path.append('/home/jamal/dev/tcc/src/models/ArmModel')
the autocomplete from PyCharm IDE couldn't find the package and I got the same error runnig the code.
I also tried to append '../' to the sys.path. When I tried the code below, I got the ModuleNotFoundError: No module named 'src'
import sys
sys.path.append('../')
from src.models.ArmModel import ArmModel
When I tried to change the import to
from models.ArmModel import ArmModel
The IDE displayed an error with >Unresolved reference under models and ArmModel