I'm attempting to include & call Python functions from a different py
file, by providing a relative file path to the os.path.join
function call.
Let's say I have two py
files, TEST1.py
and TEST2.py
, and a function defined inside of TEST2.py
called TEST3()
.
Alongside the following directory structure:
TEST1.py
|____________TEST2.py
So TEST1.py
is located one directory above TEST2.py
.
With the following code inside of TEST1.py
:
import os
CurrDirPath = os.path.dirname(__file__)
CurrFileName = os.path.join(CurrDirPath, "../TEST2.py")
import TEST2
if (__name__ == '__main__'):
print(TEST2.TEST3())
And the following code inside of TEST2.py
:
def TEST3():
return "test"
Results in the following exception upon attempting to run TEST1.py
:
import TEST2
ModuleNotFoundError: No module named 'TEST2'
What is the proper way to include Python functions from a relative file path?
Thanks for reading my post, any guidance is appreciated.