PYTEST: Module not found error
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2025 02:16 PM
Hi,
Apologies, as I am trying to use Pytest first time. I know this question has been raised but I went through previous answers but the issue still exists.
I am following DAtabricks and other articles using pytest. My structure is simple as
-tests
--common [Folder]
---- __init__.py
---- utils.py
--test_tran.py
--test_utils.py
--exectest.ipynb
Common[Folder],test_tran,test_utils and exectest files are at same level. test_utils refers utils.py
My test files work fine [test_tran and test_utils].
When I try to execute exectest notebook, I am thrown error
_______________________ ERROR collecting test_tran.py _________________________ ImportError while importing test module '/Workspace/Users/...../test/tests/test_tran.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: /usr/lib/python3.11/importlib/__init__.py:126: in import_module return _bootstrap._gcd_import(name[level:], package, level) E ModuleNotFoundError: No module named 'test_tran'
This error is thrown for both test_tran and test_utils files.
I have tried with VS Code [bundle], and it is the same result.
Code of exectest.ipynb file:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2025 09:01 AM
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 thePYTHONPATHto 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__.pyin 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.