So, I have a project structure like following.
Project
|
|--- Module1
|----- __init__.py
|----- A.py
|----- 1B.py
|---- update()
|--- Module2
|----- __init__.py
|----- C.py
|----- D.py
|--- Tests
|----- Test1.py
I am very new to modular structure in python. So all the examples I have seen, have init.py as an empty file. I suppose it needs to be there to make Module1 as module
.
I am trying to import A.py
in Test1.py
1B.py
has a class B
which has a method update(self,x)
. The 1B.py
filename is intentional because I want to know how to import file starting with a number and file starting with letter.
How can import 1B.py
which is from Module1
and then use update
method
import sys
sys.path.insert(0,"../")
class Test1(unittest.TestCase):
def test_update(self):
target = __import__("Module1.1B",fromlist=[])
print(target.Solution)
I tried
from Module1.1B ...
which throws error hence I used import where I get<module 'Module1' from '..\\Module1\\__init__.py'>
when I printtarget
Does this mean that I was successful to access module
Module1
?Also to make this work, I added sys.path.insert to the path so that it can refer to the parent folder.
What I am trying to do is that I will have a dedicated test function for every class or function in my test file.
I am not sure what I am doing wrong here.