Pytest modulenotfounderror: no module named ‘src’

The error “ModuleNotFoundError: No module named ‘src'” occurs when the pytest module is unable to find the ‘src’ module that you are trying to import.

This error typically happens when your project structure or import statements are not properly set up. Here are some possible reasons and solutions to resolve this error:

  1. Check your project structure:

    • Make sure you have a directory named ‘src’ in your project root directory.
    • Ensure that the ‘src’ directory contains the necessary Python modules or packages you are trying to import.
  2. Verify your import statements:

    • Ensure that you are using the correct import statement to import the module from ‘src’. For example, if you have a module named ‘example_module’ inside the ‘src’ directory, the import statement should be something like:
    • from src.example_module import some_function
  3. Check your PYTHONPATH:

    • Confirm that the ‘src’ directory is included in your Python’s module search path.
    • You can set the PYTHONPATH environment variable to include the path to the ‘src’ directory.
    • Alternatively, you can use relative imports if the module you are trying to import is in a subdirectory within your project structure. For example:
    • from .subdirectory.example_module import some_function

By following these steps, you should be able to resolve the ‘ModuleNotFoundError: No module named ‘src” error.

Leave a comment