cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Get Started Discussions
Start your journey with Databricks by joining discussions on getting started guides, tutorials, and introductory topics. Connect with beginners and experts alike to kickstart your Databricks experience.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

PYTEST: Module not found error

ramisinghl01
New Contributor

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:

pip install pytest
import pytest
import sys
import os, sys
repo_name = "unittest"
# Get the path to this notebook, for example "/Workspace/Repos/{username}/{repo-name}".
notebook_path = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()

# Get the repo's root directory name.
repo_root = os.path.dirname(os.path.dirname(notebook_path))

# Prepare to run pytest from the repo.
os.chdir(f"/Workspace/{repo_root}/{repo_name}")
print(os.getcwd())
# Skip writing pyc files on a readonly filesystem.
sys.dont_write_bytecode = True

# Run pytest.
retcode = pytest.main([".", "-v", "-p", "no:cacheprovider"])

# Fail the cell execution if there are any test failures.
assert retcode == 0, "The pytest invocation failed. See the log for details."

 

1 REPLY 1

mark_ott
Databricks Employee
Databricks Employee

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.

python
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:

python
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:

python
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

python
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.

Join Us as a Local Community Builder!

Passionate about hosting events and connecting people? Help us grow a vibrant local communityโ€”sign up today to get started!

Sign Up Now