pytest: Key errors while running pytest

Thanks for submitting an issue!

Here’s a quick checklist in what to include:

  • Include a detailed description of the bug or suggestion
  • pip list of the virtual environment you are using
  • pytest and operating system versions
  • Minimal example if possible

I have been seeing key errors while running pytest. I have a test suite written in pytest, and when I try to run it, I see the following errors all the time:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/_pytest/config.py", line 328, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/Users/nbharadwaj/code/thewall/tests/test_sign_out.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/_pytest/config.py", line 328, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/Users/nbharadwaj/code/thewall/tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/_pytest/config.py", line 359, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/Users/nbharadwaj/code/thewall/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/_pytest/config.py", line 365, in _importconftest
    mod = conftestpath.pyimport()
  File "/usr/local/lib/python3.6/site-packages/py/_path/local.py", line 668, in pyimport
    __import__(modname)
  File "/usr/local/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 213, in load_module
    py.builtin.exec_(co, mod.__dict__)
  File "/Users/nbharadwaj/code/thewall/conftest.py", line 6, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'
ERROR: could not load /Users/nbharadwaj/code/thewall/conftest.py

My system was is running 2 versions of python. Python 2.7 and Python 3.6

Python 3.6 is what my test suite is written on. I have tried uninstalling and reinstalling python3.6 multiple times, but have not had any luck with it so far. I am using pycharm as my IDE.

pytest version: 3.3.2 operating system: MacOS Sierra 10.12.6

Can someone please help me on this?


edited by @The-Compiler to add code tags

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Try deleting all your .pyc files: https://github.com/pytest-dev/pytest/issues/3058#issuecomment-359282653

This is a problem I’ve seen when using the same project folder with two very different environment contexts. In my case, it happens when switching between Mac OS and Docker; .pyc files created in one environment stick around in the folder, and when running python in the other environment, it chokes with similar errors. I think it’s because the path to the folder has changed.

please start using the actual github code formatting instead of dumping unreadable massive posts 😠

I’ve come to the same github issue, when I clearly had no reason to. Because of that, I think this is a pytest issue, in that the error message should make it clearer that it’s not a pytest issue, so people like me don’t end up here.

The reason I thought it was a pytest issue is that the first couple exceptions were key errors, and all others occur “during handling of the above exception”. Can this be fixed?

Some message along the lines of “WARN: the error actually seems to originate with your code, disregard KeyErrors” would have saved some time 😃

Yep, that change landed in 3.9. So yeah, you did indeed clearly not have any reason to come to this issue 😉

Traceback (most recent call last): File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 334, in _getconftestmodules return self._path2confmods[path] KeyError: local(‘/home/sreedeep/pytest_fixtures/Tstorm_Advance/tests/home/login_tests.py’)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 334, in _getconftestmodules return self._path2confmods[path] KeyError: local(‘/home/sreedeep/pytest_fixtures/Tstorm_Advance/tests/home’)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 365, in _importconftest return self._conftestpath2mod[conftestpath] KeyError: local(‘/home/sreedeep/pytest_fixtures/Tstorm_Advance/tests/conftest.py’)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/bin/pytest”, line 11, in <module> sys.exit(main()) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 52, in main config = _prepareconfig(args, plugins) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 163, in _prepareconfig pluginmanager=pluginmanager, args=args) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 617, in call return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 222, in _hookexec return self._inner_hookexec(hook, methods, kwargs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 216, in <lambda> firstresult=hook.spec_opts.get(‘firstresult’), File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 196, in _multicall gen.send(outcome) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/helpconfig.py”, line 68, in pytest_cmdline_parse config = outcome.get_result() File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 76, in get_result raise ex[1].with_traceback(ex[2]) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 180, in _multicall res = hook_impl.function(*args) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 936, in pytest_cmdline_parse self.parse(args) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 1086, in parse self._preparse(args, addopts=addopts) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 1057, in _preparse args=args, parser=self._parser) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 617, in call return self._hookexec(self, self._nonwrappers + self._wrappers, kwargs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 222, in _hookexec return self._inner_hookexec(hook, methods, kwargs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 216, in <lambda> firstresult=hook.spec_opts.get(‘firstresult’), File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 201, in _multicall return outcome.get_result() File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 76, in get_result raise ex[1].with_traceback(ex[2]) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 180, in _multicall res = hook_impl.function(*args) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 982, in pytest_load_initial_conftests self.pluginmanager._set_initial_conftests(early_config.known_args_namespace) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 317, in _set_initial_conftests self._try_load_conftest(anchor) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 323, in _try_load_conftest self._getconftestmodules(anchor) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 337, in _getconftestmodules clist = self._getconftestmodules(path.dirpath()) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 348, in _getconftestmodules mod = self._importconftest(conftestpath) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 387, in _importconftest self.consider_conftest(mod) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 410, in consider_conftest self.register(conftestmodule, name=conftestmodule.file) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 255, in register ret = super(PytestPluginManager, self).register(plugin, name) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 254, in register hook._maybe_apply_history(hookimpl) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 650, in _maybe_apply_history res = self._hookexec(self, [method], kwargs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 222, in _hookexec return self._inner_hookexec(hook, methods, kwargs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/init.py”, line 216, in <lambda> firstresult=hook.spec_opts.get(‘firstresult’), File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 201, in _multicall return outcome.get_result() File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 76, in get_result raise ex[1].with_traceback(ex[2]) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/pluggy/callers.py”, line 180, in _multicall res = hook_impl.function(*args) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/tests/conftest.py”, line 26, in pytest_addoption parser.addoption(“–browser”) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 529, in addoption self._anonymous.addoption(*opts, **attrs) File “/home/sreedeep/pytest_fixtures/Tstorm_Advance/venv/lib/python3.6/site-packages/_pytest/config.py”, line 754, in addoption raise ValueError(“option names %s already added” % conflict) ValueError: option names {‘–browser’} already added

I’m closing this for now because of lack of feedback, but feel free to follow up if there is more information available.

It looks like an issue reported by Pandas while parsing your test.json - this is entirely unrelated to pytest though, and you’d probably get the same error if you just run your conftest.py with python.

The problem seems to be:

ModuleNotFoundError: No module named 'pandas'

You are importing pandas in "/Users/nbharadwaj/code/thewall/conftest.py", line 6 but that doesn’t seem to be installed. Try executing:

sudo pip install pandas

(I see you are not using a virtual environment, I would recommend to use them in the future)