Python Forum
Importing modules from different folders - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Importing modules from different folders (/thread-37558.html)



Importing modules from different folders - Tomli - Jun-25-2022

My program has the following structure:

Program
├── src
    ├── <classes>
├── test
    ├── <myTests>
I'm trying to use unittest to make tests for the classes inside my src folder. I'm having so many problems, relative imports problems, a "ModuleNotFoundError: No module named 'test.unit' ", running 0 of all my tests like they don't exist.

At the moment this is the dummy test I'm trying to make work:
#unit.py

#built-in imports
import unittest

#local classes
from ..src.components.Containers.ContainerEmployees import ContainerEmployees

class ContainerEmployeesTest(unittest.TestCase):
    def TestPass(self):
        containerEmployees = ContainerEmployees()
        
        print(containerEmployees)
        self.assertEqual(1,1)


    def TestFails(self):
        self.assertEqual(1,2)
I'm completely lost, please someone tell how would you do this? how would you import them into your tests? and then how would you execute those tests? Please I need an example of this


RE: Importing modules from different folders - Axel_Erfurt - Jun-25-2022

Containers and ContainerEmployees are folders or classes in src?

if classes try

from src.components import Containers, ContainerEmployees


RE: Importing modules from different folders - Pedroski55 - Jun-26-2022

Python just has to know where to look. Python has it's standard paths to check.

If you want to add some other paths temporarily, you can do something like this, without adding the path permanently to your system $PATH

import sys
    # sys.path.append('/path/to/my/things/')
    sys.path.append('/home/pedro/myPython/myModules/')

    # this makes A4 size AFs individual for each student
    #import make_OE_ClassesA4_AFs
    
    # this one makes A4 size for the groups
    import make_OE_ClassesA4_AFs
On Linux systems you will have a file .bashrc in your home folder. You can permanently set look-up paths there.


RE: Importing modules from different folders - snippsat - Jun-26-2022

Some advice on setup,and use pytest🎭
program folder should be the package folder,
then as a advice should keep test folder should be outside of it.
Also if use poetry get this similar setup(with pytest) for free.
Look at how Requests GitHub is setup.
project_env
  program\
    src\
      |--universe.py      
  test\
    |--test_answer.py
universe.py:
class Answer:
    def __init__(self, answer):
        self.answer = answer
        
    def __repr__(self) -> str:
        return f'Answer({self.answer})'

if __name__ == '__main__':
    obj = Answer(42)
    print(obj.answer)    
test_answer.py:
from program.src import universe

def test_answer(): 
    obj = universe.Answer(42)    
    assert obj.answer == 42
A couple of run command line.
(project_env) G:\div_code\project_env
λ pytest -v
=========== test session starts ============
platform win32 -- Python 3.10.5, pytest-7.1.2, pluggy-1.0.0 -- G:\div_code\project_env\Scripts\python.exe
cachedir: .pytest_cache
rootdir: G:\div_code\project_env
collected 1 item

test/test_answer.py::test_answer PASSED    [100%]

========== 1 passed in 2.85s =============
make a error.
(project_env) G:\div_code\project_env
λ pytest -v
============ test  session starts ============ 
platform win32 -- Python 3.10.5, pytest-7.1.2, pluggy-1.0.0 -- G:\div_code\project_env\Scripts\python.exe
cachedir: .pytest_cache
rootdir: G:\div_code\project_env
collected 1 item

test/test_answer.py::test_answer FAILED    [100%]

============ FAILURES ============
____________ test_answer ____________

    def test_answer():
        obj = universe.Answer(100)
>       assert obj.answer == 42
E       assert 100 == 42
E        +  where 100 = Answer(100).answer

============ short test summary info ============ 
test\test_answer.py:5: AssertionError 
FAILED test/test_answer.py::test_answer - assert 100 == 42
=========== 1 failed in 3.00s ===========