Answer:
There might be related questions and answers.
For this particular problem, these are three possible solutions.
Assuming the following folder structure:
project_root/
lib/
utils.py
tests/
test_market_rates/
test_market_rates.py
Solution 1: Run file from Root Folder
Run the script from project_root
. I.e.:
In Terminal (e.g. bash)
> pwd
~/project_root
> python3 tests/test_market_rates/test_market_rates.py
Hello World!
Solution 2: Create a proper package out of "lib"
Make a package out of lib
.
I.e. make it "pip installable", so that you can run pip install -e .
(local install). Then you will be able to call this file from both:
project_root
& tests/test_market_rates
E.g.: https://betterscientificsoftware.github.io/python-for-hpc/tutorials/python-pypi-packaging/
In general, search for: "how to create a python package".
Solution 3: Add Relative Paths (Not Good Practice).
To the file test_market_rates.py
add the following lines of code (before the lib import statement).
import sys
import os
FILE_DIRECTORY=os.path.dirname(os.path.realpath(__file__)) #path to test_market_rates.py
sys.path.append(os.path.join(FILE_DIRECTORY,"..","..")) #adds to PATH
from lib.utils import say_hello
You will be able to call this file from both:
project_root
& tests/test_market_rates
Reference: Find the current directory and file's directory