Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key errors while running pytest #3112

Closed
4 tasks
nbharadwaj11 opened this issue Jan 12, 2018 · 16 comments
Closed
4 tasks

Key errors while running pytest #3112

nbharadwaj11 opened this issue Jan 12, 2018 · 16 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@nbharadwaj11
Copy link

nbharadwaj11 commented Jan 12, 2018

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

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jan 12, 2018
@pytestbot
Copy link
Contributor

GitMate.io thinks the contributor most likely able to help you is @The-Compiler.

@nicoddemus
Copy link
Member

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)

@nicoddemus nicoddemus added type: question general question, might be closed after 2 weeks of inactivity and removed type: bug problem that needs to be addressed labels Jan 12, 2018
@nbharadwaj11
Copy link
Author

I installed Pandas, and now I see the following errors:
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 "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 656, in _load_unlocked
File "", line 626, in _load_backward_compatible
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 24, in
dataValues = pd.read_json("test.json")
File "/usr/local/lib/python3.6/site-packages/pandas/io/json/json.py", line 366, in read_json
return json_reader.read()
File "/usr/local/lib/python3.6/site-packages/pandas/io/json/json.py", line 467, in read
obj = self._get_object_parser(self.data)
File "/usr/local/lib/python3.6/site-packages/pandas/io/json/json.py", line 484, in _get_object_parser
obj = FrameParser(json, **kwargs).parse()
File "/usr/local/lib/python3.6/site-packages/pandas/io/json/json.py", line 576, in parse
self._parse_no_numpy()
File "/usr/local/lib/python3.6/site-packages/pandas/io/json/json.py", line 793, in _parse_no_numpy
loads(json, precise_float=self.precise_float), dtype=None)
ValueError: Unexpected character found when decoding 'true'
ERROR: could not load /Users/nbharadwaj/code/thewall/conftest.py

There's nothing wrong with the testsuite, because it seems to be running just fine for a few of my friends.

@The-Compiler
Copy link
Member

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.

@nicoddemus
Copy link
Member

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

@Das-Sreedeep
Copy link

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

@Das-Sreedeep
Copy link

getting the same issue

@Das-Sreedeep
Copy link

Conftest.py

import pytest
from base.webdriverfactory import WebDriverFactory

@pytest.yield_fixture()
def setUp():
print("Running method level setup")
yield
print("Running method level tearDown")

@pytest.yield_fixture(scope="class")
def oneTimeSetup(request, browser):
print("Running one time setUp")
wdf = WebDriverFactory(browser)
driver = wdf.getWebDriverInstance()

# Added to the class attribute
if request.cls is not None:
    request.cls.driver = driver

yield driver #Return to the place where fixture is used
driver.quit() #Tear down the browser
print("Running one time tearDown")

#Adding parser
def pytest_addoption(parser):
parser.addoption("--browser")
parser.addoption("--osType", help="Type of operating system")

@pytest.fixture(scope="session")
def browser(request):
return request.config.getoption("--browser")

@pytest.fixture(scope="session")
def osType(request):
return request.config.getoption("--osType")

@RonnyPfannschmidt
Copy link
Member

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

@yozlet
Copy link

yozlet commented Jul 13, 2018

Try deleting all your .pyc files: #3058 (comment)

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.

@towc
Copy link

towc commented Apr 23, 2019

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 :)

@The-Compiler
Copy link
Member

@towc The output for the scenario mentioned in this issue already got improved, see #3332. What pytest version are you on, and what's the exact output you get?

@towc
Copy link

towc commented Apr 23, 2019

oh, sorry. 3.8.2, guess I better update :)

@The-Compiler
Copy link
Member

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

@Azee77
Copy link

Azee77 commented Jun 24, 2020

Any resolution for this issue?
#3112 (comment)

pytest 5.0
running pytest command from pytest.ini directory, there is only one conftest.py inside the directory
Tried clearing all .pyc files, pycache directories, didnt help.

Using custom pyenv: Is this the culprit?

@The-Compiler
Copy link
Member

@Azee77 I doubt you're seeing the same issue, given that the stacktrace above points to a custom conftest.py. Can you show the actual output you get?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

9 participants