Python Forum
Importing modules from different folders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing modules from different folders
#1
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
Reply
#2
Containers and ContainerEmployees are folders or classes in src?

if classes try

from src.components import Containers, ContainerEmployees
Reply
#3
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.
Reply
#4
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 ===========
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  confusion on importing modules carter187 4 425 Mar-03-2024, 08:11 PM
Last Post: snippsat
  Importing modules issue mp3909 9 3,565 Jun-24-2020, 10:07 PM
Last Post: snippsat
  sub-folders in folders from text line jenost 1 1,579 Mar-31-2020, 07:16 AM
Last Post: ndc85430
  Importing Custom Modules in Python 3 Flexico 1 2,600 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Trouble importing modules on a new python version snackman_barry 2 2,578 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  importing modules PiaNa 1 1,972 Jun-24-2019, 12:50 PM
Last Post: ichabod801
  Importing all modules and using it rohitnirantar 2 2,557 Aug-28-2018, 08:15 PM
Last Post: snippsat
  Importing modules SBachar 2 3,063 Apr-06-2018, 09:08 PM
Last Post: snippsat
  Importing modules Pistolpete 2 2,716 Nov-29-2017, 05:24 PM
Last Post: nilamo
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,467 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020