I am trying to import a module given an absolute path. I have read through some posts and I chose to follow one method mentioned in https://stackoverflow.com/a/53311583/19947267 but for some reason it doesnt work; below is my directory. my example directory looks like this:
<root>
|
+- /std1
| |
| +- main.py
|
+- /std2
| |
| +- lib.py
I cd to /std1
and now I want to import a module from lib.py
, but when I implement the following code:
import sys
import os
configfile = '~/std2/'
sys.path.append(os.path.dirname(os.path.expanduser(configfile)))
from std2.lib import target_module
It cant seem to recognise std2.lib, the error message was
ModuleNotFoundError: No module named 'std2.lib'
is there something I did wrong here? Can anyone please help?