{"id":943,"hash":"1100a054c80317ee485de3308a0ed8726a8990b13cc62ec45c3f0a2406556d39","pattern":"ModuleNotFoundError with pytest","full_message":"I want my tests folder separate to my application code. My project structure is like so\n\nmyproject/\n  myproject/\n    myproject.py\n    moduleone.py\n  tests/\n    myproject_test.py\n\nmyproject.py\n\nfrom moduleone import ModuleOne\n\nclass MyProject(object)\n....\n\nmyproject_test.py\n\nfrom myproject.myproject import MyProject\nimport pytest\n\n...\n\nI use myproject.myproject since I use the command\n\npython -m pytest\n\nfrom the project root directory ./myproject/\n\nHowever, then the imports within those modules fail with\n\n  E   ModuleNotFoundError: No module named 'moduleone'\n\nI am running Python 3.7 and have read that since 3.3, empty __init__ files are no longer needed which means my project becomes an implicit namespace package\n\nHowever, I have tried adding an __init__.py file in myproject/myproject/ and also tried adding a conftest.py file in myproject/ but  neither works\n\nI have read answers that say to mess with the paths and then upvoted comments in other questions saying not to.\n\nWhat is the correct way and what am I missing?\n\nEDIT;\n\nPossibly related, I used a requirements.txt to install pytest using pip. Could this be related? And if so, what is the correct way to install pytest in this case?\n\nEDIT 2:\n\nOne of the paths in sys.path is /usr/src/app/ which is a docker volume lined to /my/local/path/myproject/.\n\nShould the volume be /my/local/path/myproject/myproject/ instead?","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"In 2023.02, according to the document of pytest, you can simply add following config to your pyproject.toml to solve this problem\n\n[tool.pytest.ini_options]\npythonpath = \"src\"\naddopts = [\n    \"--import-mode=importlib\",\n]","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/54895002/modulenotfounderror-with-pytest","votes":113,"created_at":"2026-04-19T04:52:04.239291+00:00","updated_at":"2026-04-19T04:52:04.239291+00:00"}