0

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

  • Are you using Python 2 or Python 3? – ChrisGPT was on strike Feb 09 '20 at 13:14
  • I'm using Python 3.6.9 – Fernando Augusto Feb 09 '20 at 13:27
  • @Chris I did what was said and I the same error: >ModuleNotFoundError: No module named 'src'. Looks like my python can't understand 2 folders depth. Maybe I should limit its structure to src/models.py with all classes inside the models.py src/tests.py with all tests inside it Working with a python package inside another is being a pain. – Fernando Augusto Feb 09 '20 at 18:37
  • "I did what was said"—what, _exactly_, did you try? It's always better to show us than to tell us. Please read [ask]. – ChrisGPT was on strike Feb 09 '20 at 19:03
  • @Chris I updated my question with everything I did to solve it. I could solve it using the base path to my project as following: ``` sys.path.append('home/jamal/Documents/dev/tcc') from src.models.Arm import Arm ``` I still don't know why the full path to the module wasn't working. At least it's working now. – Fernando Augusto Feb 15 '20 at 14:55

1 Answers1

0

To solve this issue, I append the path to the project root file "TCC"

sys.path.append('home/jamal/Documents/dev/tcc') 
from src.models.Arm import Arm

Now I can access all the modules using "src."