Import Module Using Relative Paths
Let me start off by saying I know this issue has already been discussed, but I could not find a solution that was similar to my case. I have a directory structure like the followin
Solution 1:
Using PYTHONPATH=.
, go to the directory one level up from "project".
Invoke your code with somewhat long names, e.g. $ python -m project.tools.my_tool.tool_name
.
That way "tools" code will be allowed to access "tests", since it's still within "project".
Solution 2:
tool_name.py
print("tool_name.py imported")
constants.py
print("constants.py imported")
check_lib.py
print("check_lib.py imported")
my_test.py
import constants
import common.check_lib
from sys import path as external_import
external_import.append("../../tools/my_tool/")
import tool_name
print("my_test.py is running")
output :
constants.py imported
check_lib.py imported
tool_name.py imported
my_test.py is running
Post a Comment for "Import Module Using Relative Paths"