ImportError: cannot import name signature

11,797

Solution 1

  • 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).

Solution 2

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.

Share:
11,797
Bin
Author by

Bin

Updated on June 05, 2022

Comments

  • Bin
    Bin almost 2 years

    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.