pypipython-3.x95% confidence\u2191 113

ModuleNotFoundError with pytest

Full error message
I want my tests folder separate to my application code. My project structure is like so

myproject/
  myproject/
    myproject.py
    moduleone.py
  tests/
    myproject_test.py

myproject.py

from moduleone import ModuleOne

class MyProject(object)
....

myproject_test.py

from myproject.myproject import MyProject
import pytest

...

I use myproject.myproject since I use the command

python -m pytest

from the project root directory ./myproject/

However, then the imports within those modules fail with

  E   ModuleNotFoundError: No module named 'moduleone'

I 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

However, I have tried adding an __init__.py file in myproject/myproject/ and also tried adding a conftest.py file in myproject/ but  neither works

I have read answers that say to mess with the paths and then upvoted comments in other questions saying not to.

What is the correct way and what am I missing?

EDIT;

Possibly 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?

EDIT 2:

One of the paths in sys.path is /usr/src/app/ which is a docker volume lined to /my/local/path/myproject/.

Should the volume be /my/local/path/myproject/myproject/ instead?

In 2023.02, according to the document of pytest, you can simply add following config to your pyproject.toml to solve this problem [tool.pytest.ini_options] pythonpath = "src" addopts = [ "--import-mode=importlib", ]

API access

Get this solution programmatically \u2014 free, no authentication.

curl https://depscope.dev/api/error/1100a054c80317ee485de3308a0ed8726a8990b13cc62ec45c3f0a2406556d39
hash \u00b7 1100a054c80317ee485de3308a0ed8726a8990b13cc62ec45c3f0a2406556d39
ModuleNotFoundError with pytest — DepScope fix | DepScope