ModuleNotFoundError when python class is moved to a subdirectory

53 views Asked by At

I have the following directory structure in my python project

enter image description here

When I try to execute the test_market_rates.py file I get an Module not found error

ModuleNotFoundError: No module named 'lib'

But if I move the test_market_rates.py file to the main directory it will work without any errors

enter image description here

The following is how I have imported the module in test_market_rates.py file

from lib.utils import say_hello

print(say_hello())
1

There are 1 answers

1
Kolyan1 On

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