pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.37k stars 344 forks source link

Test works on local machine but in Docker I get "no module named 'test' " #1019

Open NiklasRz opened 2 years ago

NiklasRz commented 2 years ago

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?

pkimber commented 2 years ago

Just to say, I am getting the same issue. Did you find a solution?

pkimber commented 2 years ago

I am finding the issue in a Django project (which is the same as you I guess). I solved the issue by copying manage.py into the root folder of my project.

mohitCodepy commented 1 year ago

@pkimber , I also resolved my issue by doing same.

derek-adair commented 1 year ago

Beyond this being a question with insufficient detail supplied; this should be closed as its in no way related to this project; this kind of question belongs on a place like stack overflow or even better one of the linux related stack exchanges.


That being said;

  1. somehow copy all of the files to your docker WORKDIR
  2. install your requirements.txt (probably what OP is missing) with pip

OR you can check out one of the many examples of using python/docker in my repository history. I have a python dev image i've been using to code on for several years now if you need some concrete guidance.