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

Importing anydbm within pytest fails #2248

pfhayes opened this Issue Feb 15, 2017 · 2 comments


None yet
2 participants

pfhayes commented Feb 15, 2017

When trying to import the anydbm module from within pytest on a machine that does not have bsddb installed, the import will fail. This appears to be because

  • Importing anydbm tries to import dbhash which imports bsddb. If bsddb is not present on the machine, this fails with an ImportError, which anydbm catches and silences.
  • When running pytest, importing a module is overridden by load_module in _pytest/assertion/
  • When the import of dbhash fails, it tries to del sys.modules['dbhash'] but this throws a KeyError since 'dbhash' is not present in sys.modules
  • anydbm fails to catch the KeyError since it is expecting ImportError

Observed in pytest 3.0.5 on both OS X and Ubuntu.

This can be reproduced by including import anydbm in any test file. For example

import anydbm

def test_run():
  assert True

This error occurs: in <module>
    import anydbm
../.virtualenvs/test2/lib/python2.7/site-packages/_pytest/assertion/ in load_module
    py.builtin.exec_(co, mod.__dict__)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ in <module>
    _mod = __import__(_name)
../.virtualenvs/test2/lib/python2.7/site-packages/_pytest/assertion/ in load_module
    del sys.modules[name]
E   KeyError: 'dbhash'
$ pip list
appdirs (1.4.0)
packaging (16.8)
pip (9.0.1)
py (1.4.32)
pyparsing (2.1.10)
pytest (3.0.5)
setuptools (34.2.0)
six (1.10.0)
wheel (0.30.0a0)

This comment has been minimized.


pfhayes commented Feb 15, 2017

PR added here: #2249

@nicoddemus nicoddemus closed this Mar 10, 2017


This comment has been minimized.


nicoddemus commented Mar 10, 2017

Thanks again @pfhayes!

This was referenced Mar 6, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment