Resolving Error “ImportError: No module name named hypothesis”

When running the test script “relu_op_test.py” to verify Caffe2 installation, you may encounter this error “ImportError: No module name named hypothesis”. Let’s take a look at the content of the script to get some idea about the root cause.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from caffe2.python import core
from hypothesis import given
import hypothesis.strategies as st
import caffe2.python.hypothesis_test_util as hu
import caffe2.python.mkl_test_util as mu
import numpy as np

import unittest

class TestRelu(hu.HypothesisTestCase):

    @given(X=hu.tensor(),
           engine=st.sampled_from(["", "CUDNN"]),
           **mu.gcs)
    def test_relu(self, X, gc, dc, engine):
        op = core.CreateOperator("Relu", ["X"], ["Y"], engine=engine)
        # go away from the origin point to avoid kink problems
        X += 0.02 * np.sign(X)
        X[X == 0.0] += 0.02
        self.assertDeviceChecks(dc, op, [X], [0])
        self.assertGradientChecks(gc, op, [X], 0, [0])


if __name__ == "__main__":
    unittest.main()

A trained Python developer will immediately note that the statement in line 7 suggests that hypothesis is a python module. The resolve is as simple as installing the hypothesis module so that the script can be properly executed.

The command to install the module is a one-liner as follows:

$ pip install hypothesis

For system-wide installation, we add sudo to the command

$ sudo pip install hypothesis

This should in theory resolve the error.

As a side note, hypothesis is indeed a Python library that is used for property-based testing.

Leave a Reply

Your email address will not be published. Required fields are marked *