Bin
Bin

Reputation: 3834

ImportError: cannot import name signature

I am tying to modify the original sklearn.CalibrationCV to create my won version. The original code has "from .utils.fixes import signature". So I did the following in my version:

from sklearn.utils.fixes import signature

but got a error:

ImportError: cannot import name signature

When check the sklearn source code on GitHub. I see the following code inside fixes.py:

try:
    from inspect import signature
except ImportError:
    from ..externals.funcsigs import signature

Then I did from inspect import signature directly. Still get "ImportError: cannot import name signature"

Besides how to fix this, I am also curious about why the original version can import a module that will be imported from another source? Thanks.

Upvotes: 3

Views: 14375

Answers (2)

MSS
MSS

Reputation: 3623

The accepted answer doesn't work with the latest version of sklearn.

Please install funcsigs directly using

pip install funcsigs

and use from funcsigs import signature instead.

Upvotes: 2

dnozay
dnozay

Reputation: 24314

  • In python 2, the inspect module does not have a signature method.
  • In python 3, the inspect module does have a signature method.

This code is just trying to work with both python 2 and 3.

You may want to use the funcsigs module if you are using python 2, or use sklearn.externals.funcsigs directly (for version sklearn >= 0.17).

Upvotes: 3

Related Questions