Your issue with ModuleNotFoundError: No module named 'test_tran' when running pytest from a notebook is likely caused by how Python sets the module import paths and the current working directory inside Databricks notebooks (or similar environments). This is different from a traditional shell or VS Code terminal. Even though your file structure is correct and works for direct test execution, the import path can break in notebooks due to how Python resolves packages.
Why Does This Happen?
-
Python uses the current working directory (os.getcwd()) and the PYTHONPATH to resolve imports.
-
When you run pytest inside a notebook, the current directory might not be set as expected, so relative imports or direct module names can fail.
-
Pytest also sometimes changes the working directory before running tests, which can confuse imports.
Solutions
1. Use the Correct PYTHONPATH
Set PYTHONPATH to your repo or test directory before running pytest. This will help Python locate your test files and modules.
import os, sys
repo_root = "/Workspace/Users/...../test" # Set this explicitly if needed
sys.path.insert(0, repo_root)
print(sys.path)
Add this before you import pytest or run your tests. If you move to the test directory using os.chdir, also add its parent folder to sys.path.
2. Reference Modules with Package Notation
If your folder is named tests, you should reference modules as tests.test_tran not simply test_tran.
For example, in your notebook:
import tests.test_tran
import tests.common.utils
Make sure each folder has an __init__.py file so Python sees them as packages.
3. Use Test Discovery Pattern
When running pytest, try specifying the path relative to where imports work:
retcode = pytest.main(["tests/", "-v", "-p", "no:cacheprovider"])
4. Check Test File Names
Ensure files and folders don't have names that shadow built-in modules (test.py, string.py, etc.) and have valid Python syntax.
Example Notebook Correction
import sys, os, pytest
repo_root = "/Workspace/Users/...../test"
sys.path.insert(0, repo_root) # Add repo root for resolving imports
os.chdir(repo_root) # Move to repo root
print(f"Working Directory: {os.getcwd()}")
print(f"PYTHONPATH: {sys.path}")
sys.dont_write_bytecode = True
retcode = pytest.main(["tests/", "-v", "-p", "no:cacheprovider"])
assert retcode == 0, "The pytest invocation failed. See the log for details."
Ensure your imports inside test_tran.py and test_utils.py use either relative imports (if inside a package) or absolute imports using the full package path.
Common Pitfalls
-
Missing __init__.py: You must have an __init__.py in every folder to treat it as a package.
-
Incorrect Working Directory: If running in a notebook, always print and check os.getcwd() before and after changes.
-
Sys.path Not Updated: Always add the repo directory to sys.path if importing modules from sibling folders.
Try these suggestions to correct the ModuleNotFoundError. If it still fails, post your exact folder structure and contents of your imports for a tailored fix.