Published on

[Solved] Pytest Error: ImportError: Error importing plugin '': No module named ...

Problem:

Running into the following error when trying to run a Pytest test suite in a Python application via the pytest command:

 File "/usr/lib/python2.7/dist-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/usr/lib/python2.7/dist-packages/pluggy/callers.py", line 81, in get_result
    _reraise(*ex)  # noqa
  File "/usr/lib/python2.7/dist-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 739, in pytest_cmdline_parse
    self.parse(args)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 947, in parse
    self._preparse(args, addopts=addopts)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 889, in _preparse
    self.pluginmanager.consider_preparse(args)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 499, in consider_preparse
    self.consider_pluginarg(parg)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 524, in consider_pluginarg
    self.import_plugin(arg, consider_entry_points=True)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 574, in import_plugin
    six.reraise(ImportError, new_exc, tb)
  File "/usr/lib/python2.7/dist-packages/_pytest/config/__init__.py", line 565, in import_plugin
    __import__(importspec)
ImportError: Error importing plugin "sklearn.tests.random_seed": No module named sklearn.tests.random_seed

Solution:

There are a number of potential causes for this issue, the most likely being that the PYTHONPATH environment variable not being defined in your ~/.profile file. There are two potential solutions you can try here:

  1. Define the PYTHONPATH environment variable by running the command export PYTHONPATH=<your_user_defined_module_path_here>. Then retry running pytest
  2. Add the prefix python -m before the usual pytest command like so: python -m pytest

Conclusion

Well this is the end of the article, if you have any questions or concerns please feel free to post a comment in this article and I will get back to you if I find the time.

I hope you found this article helpful. Thanks so much for reading my article! Feel free to follow me on Twitter and GitHub, connect with me on LinkedIn and subscribe to my YouTube channel.