{"id":948,"hash":"81164c276c7ee2c49b08dd1c22126b08c3a79f1fe190857c230fd045ce1767e7","pattern":"pytest cannot find module","full_message":"I am following the pytest good practices or at least I think I am. However, pytest cannot find my module. It seems not to include the current directory in its PYTHONPATH.\n\nThe source file:\n\ndef add(x, y):\n    return x + y\n\nThe test file:\n\nimport pytest\nfrom junk.ook import add\n\ndef test_add_true():\n    assert add(1, 1) == 2\n\nAnd the shell output with a Python 3 virtual environment called \"p3\".\n\np3; pwd          \n/home/usr/tmp/junk\np3; ls           \ntotal 0\n0 junk/  0 tests/\np3; ls junk      \ntotal 4.0K\n4.0K ook.py     0 __init__.py\np3; ls tests \ntotal 4.0K\n4.0K test_ook.py     0 __pycache__/\np3; pytest\n============================= test session starts ==============================\nplatform linux -- Python 3.4.5, pytest-3.4.1, py-1.5.2, pluggy-0.6.0\nrootdir: /home/usr/tmp/junk, inifile:\ncollected 0 items / 1 errors                                                   \n\n==================================== ERRORS ====================================\n______________________ ERROR collecting tests/test_ook.py ______________________\nImportError while importing test module '/home/usr/tmp/junk/tests/test_ook.py'.\nHint: make sure your test modules/packages have valid Python names.\nTraceback:\ntests/test_ook.py:2: in <module>\n    from junk.ook import add\nE   ImportError: No module named 'junk'\n!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!\n=========================== 1 error in 0.08 seconds ============================\n    \n    def test_add_true():\n        assert add(1, 1) == 2\n\nHowever, running the following does work fine.\n\np3; python -m pytest tests/\n============================= test session starts ==============================\nplatform linux -- Python 3.4.5, pytest-3.4.1, py-1.5.2, pluggy-0.6.0\nrootdir: /home/usr/tmp/junk, inifile:\ncollected 1 item                                                               \n\ntests/test_ook.py .                                                      [100%]\n\n=========================== 1 passed in 0.02 seconds ===========================\n\nWhat am I doing wrong?","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"Update for pytest 7 and newer: use the pythonpath setting\nRecently, pytest has added a new core plugin that supports sys.path modifications via the pythonpath configuration value. The solution is thus much simpler now and doesn't require any workarounds anymore:\n\npyproject.toml example:\n\n[tool.pytest.ini_options]\npythonpath = [\n  \".\"\n]\n\npytest.ini example:\n\n[pytest]\npythonpath = .\n\nThe path entries are calculated relative to the rootdir, thus . adds junk directory to sys.path in this case.\n\nMultiple path entries are also allowed: for a layout\n\njunk/\n├── src/\n|   └── lib.py\n├── junk/\n│   ├── __init__.py\n│   └── ook.py\n└── tests\n     ├── test_app.py\n     └── test_lib.py\n\nthe configuration\n\n[tool.pytest.ini_options]\npythonpath = [\n  \".\", \"src\",\n]\n\nor\n\n[pytest]\npythonpath = . src\n\nwill add both lib module and junk package to sys.path, so\n\nimport junk\nimport lib\n\nwill both work.\n\nOriginal answer\nJust put an empty conftest.py file in the project root directory:\n\n$ pwd\n/home/usr/tmp/junk\n$ touch conftest.py\n\nYour project structure should become:\n\njunk\n├── conftest.py\n├── junk\n│   ├── __init__.py\n│   └── ook.py\n└── tests\n    └── test_ook.py\n\nWhat happens here: when pytest discovers a conftest.py, it modifies sys.path so it can import stuff from the conftest module. So, since now an empty conftest.py is found in rootdir, pytest will be forced to append it to sys.path. The side effect of this is that your junk module becomes importable.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/49028611/pytest-cannot-find-module","votes":83,"created_at":"2026-04-19T04:52:04.242166+00:00","updated_at":"2026-04-19T04:52:04.242166+00:00"}