Not able to import custom functions from other directory to use in unittest

17 Views Asked by At

So this is my workflow of directories

src/
   python_scripts/
      __init__.py
      pdf.py
      it_counter.py
   test/
      __init__.py
      test_pdf.py
      test_it_counter.py
   pdf_pages/
      CONTAINS PDF FILES ONLY

Now I am trying to import certain functions of pdf.py and it_counter.py file in test_it_counter.py. I have used following line to import them

from python_scripts.pdf import pdf_parser 
from python_scripts.it_counter import word_counter

I have attached my code for test_it_counter.py here:

import unittest
from reportlab.pdfgen.canvas import Canvas
import os
from python_scripts.pdf import pdf_parser
from python_scripts.it_counter import word_counter

def pdf_creater():
    test_pdf_path = "test_pdf.pdf"
    tmp_folder_path = "test/tmp_output"
    canvas = Canvas(test_pdf_path)
    page1_text = "Ganga is in itself a great way to manage files in it. It is really useful."
    page2_text = "It is good to have such tools!"
    canvas.drawString(200, 500, page1_text)
    canvas.showPage()
    canvas.drawString(200, 500, page2_text)
    canvas.save()
    pdf_parser(test_pdf_path, tmp_folder_path)
    return tmp_folder_path

class Testcounter(unittest.TestCase):

    def test_it_counter(self):
        
        folder_path = pdf_creater()
        print(f'{folder_path}')
        files = os.listdir(folder_path)
        file_paths = [os.path.join(folder_path, file) for file in files]
        
        # Check word count for each file
        self.assertEqual(word_counter(file_paths[0]), 15)
        self.assertEqual(word_counter(file_paths[1]), 6)

I am trying to execute it from src folder like this python -m unittest test/test_it_counter.py But it gives following error :

File "/home/saumysharan/miniforge3/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/saumysharan/miniforge3/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/saumysharan/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/home/saumysharan/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/saumysharan/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/saumysharan/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/saumysharan/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/saumysharan/.vscode/extensions/ms-python.debugpy-2024.2.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/home/saumysharan/GSoC/GangaGSoC2024/test/test_it_counter.py", line 8, in <module>
    test_pdf_path = "test_pdf.pdf"
ModuleNotFoundError: No module named 'python_scripts'

Seems like there is some problem with import, but i am not able to figure it out.

Any help appreciated, Thanks!

I tried adding __init__.py file to all other sub directories of parent folder but in no vain. When I tried executing test_pdf.py in same manner,I got no errors and all test were passed. But for 'test_it_counter.py' specifically it gives error.

0

There are 0 best solutions below