pytest-dev / pytest-dev/pytest-django
Test works on local machine but in Docker I get "no module named 'test' "
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
I have a pytest.ini and a test_something.py file in the same dir as my manage.py. If I run 'pytest' directly, it works. If I run the whole thing with docker-compose, get an interactive console on the docker container and run 'pytest' there, I get:
root@06070aaebbbd:/code/coco# pytest
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pytest_django/plugin.py", line 179, in _handle_import_error
yield
File "/usr/local/lib/python3.7/site-packages/pytest_django/plugin.py", line 351, in pytest_load_initial_conftests
dj_settings.DATABASES
File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line 82, in getattr
self._setup(name)
File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line 69, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.7/site-packages/django/conf/init.py", line 170, in init
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python3.7/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 953, in _find_and_load_unlocked
File "", line 219, in _call_with_frames_removed
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'test'During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/pytest", line 8, in
sys.exit(console_main())
File "/usr/local/lib/python3.7/site-packages/_pytest/config/init.py", line 187, in console_main
code = main()
File "/usr/local/lib/python3.7/site-packages/_pytest/config/init.py", line 145, in main
config = _prepareconfig(args, plugins)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/init.py", line 325, in _prepareconfig
pluginmanager=pluginmanager, args=args
File "/usr/local/lib/python3.7/site-packages/pluggy/_hooks.py", line 265, in call
return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
File "/usr/local/lib/python3.7/site-packages/pluggy/_manager.py", line 80, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/usr/local/lib/python3.7/site-packages/pluggy/_manager.py", line 80, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/usr/local/lib/python3.7/site-packages/pluggy/_callers.py", line 55, in _multicall
gen.send(outcome)
File "/usr/local/lib/python3.7/site-packages/_pytest/helpconfig.py", line 102, in pytest_cmdline_parse
config: Config = outcome.get_result()
File "/usr/local/lib/python3.7/site-packages/pluggy/_result.py", line 60, in get_result
raise ex[1].with_traceback(ex[2])
File "/usr/local/lib/python3.7/site-packages/pluggy/_callers.py", line 39, in _multicall
res = hook_impl.function(*args)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/init.py", line 1016, in pytest_cmdline_parse
self.parse(args)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/init.py", line 1304, in parse
self._preparse(args, addopts=addopts)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/init.py", line 1207, in _preparse
early_config=self, args=args, parser=self._parser
File "/usr/local/lib/python3.7/site-packages/pluggy/_hooks.py", line 265, in call
return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
File "/usr/local/lib/python3.7/site-packages/pluggy/_manager.py", line 80, in _hookexec
return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
File "/usr/local/lib/python3.7/site-packages/pluggy/_callers.py", line 60, in _multicall
return outcome.get_result()
File "/usr/local/lib/python3.7/site-packages/pluggy/_result.py", line 60, in get_result
raise ex[1].with_traceback(ex[2])
File "/usr/local/lib/python3.7/site-packages/pluggy/_callers.py", line 39, in _multicall
res = hook_impl.function(*args)
File "/usr/local/lib/python3.7/site-packages/pytest_django/plugin.py", line 351, in pytest_load_initial_conftests
dj_settings.DATABASES
File "/usr/local/lib/python3.7/contextlib.py", line 130, in exit
self.gen.throw(type, value, traceback)
File "/usr/local/lib/python3.7/site-packages/pytest_django/plugin.py", line 183, in _handle_import_error
raise ImportError(msg)
ImportError: No module named 'test'
Any idea what might be the problem here?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with pytest.ini, manage.py, test_something.py, and the docker-compose setup; compare the directory and environment when pytest runs locally versus in the container, using the pytest_django/plugin.py traceback as the entry point. Done means the same test command resolves the Django settings module and reaches the test suite inside Docker.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, docker, docker-compose, python
- Domain
- devops, testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100