'import from import in different folders in python

My directory structure is like, [1]: https://i.stack.imgur.com/Jk7II.png

python_file_without_extension contains

from py_file import function

In test_app.py, I want to write unit tests for python_file_without_extension and this is how I tried importing python_file_without_extension

import imp
import os

dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = dir_path.replace('test', 'app/python_file_without_extension')

file = imp.load_source('python_file_without_extension', file_path)

def test_func():
  #write test
   pass

I am getting error

from py_file import function
E   ModuleNotFoundError: No module named 'py_file'


Solution 1:[1]

Try in your test file:

import sys
sys.path.insert(0, "path to your main folder")
from app.py_file import py_func

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cao-nv