Cannot import name 'Decoder'

Summary of what happened:

Hi! guys,
when I tried to import Decoder by from nilearn.decoding import Decoder, the error message showed that ImportError: cannot import name 'Decoder'. Then I checked the package in my environment, the version of nilearn seems the latest:

# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
argparse                  1.4.0                    pypi_0    pypi
atk-1.0                   2.36.0               h3371d22_4    conda-forge
attrs                     22.2.0                   pypi_0    pypi
brotli                    1.0.9                h166bdaf_7    conda-forge
brotli-bin                1.0.9                h166bdaf_7    conda-forge
brotlipy                  0.7.0           py39hb9d737c_1004    conda-forge
bzip2                     1.0.8                h7f98852_4    conda-forge
c-ares                    1.18.1               h7f98852_0    conda-forge
ca-certificates           2022.9.24            ha878542_0    conda-forge
cached-property           1.5.2                hd8ed1ab_1    conda-forge
cached_property           1.5.2              pyha770c72_1    conda-forge
cairo                     1.16.0            ha61ee94_1014    conda-forge
certifi                   2022.9.24          pyhd8ed1ab_0    conda-forge
cffi                      1.15.1           py39he91dace_0    conda-forge
charset-normalizer        2.1.1              pyhd8ed1ab_0    conda-forge
ci-info                   0.2.0              pyh9f0ad1d_0    conda-forge
click                     8.1.3            py39hf3d152e_0    conda-forge
contourpy                 1.0.5            py39hf939315_0    conda-forge
cryptography              38.0.3           py39h3ccb8fc_0    conda-forge
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
etelemetry                0.3.0              pyhd8ed1ab_0    conda-forge
expat                     2.4.9                h27087fc_0    conda-forge
filelock                  3.8.0              pyhd8ed1ab_0    conda-forge
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 hab24e00_0    conda-forge
fontconfig                2.14.0               hc2a2eb6_1    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.37.3           py39hb9d737c_0    conda-forge
freetype                  2.12.1               hca18f0e_0    conda-forge
fribidi                   1.0.10               h36c2ea0_0    conda-forge
gdk-pixbuf                2.42.8               hff1cb4f_1    conda-forge
gettext                   0.19.8.1          h27087fc_1009    conda-forge
giflib                    5.2.1                h36c2ea0_2    conda-forge
graphite2                 1.3.13            h58526e2_1001    conda-forge
graphviz                  6.0.1                h5abf519_0    conda-forge
gtk2                      2.24.33              h90689f9_2    conda-forge
gts                       0.7.6                h64030ff_2    conda-forge
h5py                      3.7.0           nompi_py39hd51670d_101    conda-forge
harfbuzz                  5.2.0                hf9f4e7c_0    conda-forge
hdf5                      1.12.2          nompi_h4df4325_100    conda-forge
html5lib                  1.1                pyh9f0ad1d_0    conda-forge
icu                       70.1                 h27087fc_0    conda-forge
idna                      3.4                pyhd8ed1ab_0    conda-forge
importlib-metadata        4.11.4           py39hf3d152e_0    conda-forge
isodate                   0.6.1              pyhd8ed1ab_0    conda-forge
joblib                    1.2.0              pyhd8ed1ab_0    conda-forge
jpeg                      9e                   h166bdaf_2    conda-forge
jsonschema                4.17.3                   pypi_0    pypi
keyutils                  1.6.1                h166bdaf_0    conda-forge
kiwisolver                1.4.4            py39hf939315_0    conda-forge
krb5                      1.19.3               h08a2579_0    conda-forge
lcms2                     2.12                 hddcbb42_0    conda-forge
ld_impl_linux-64          2.36.1               hea4e1c9_2    conda-forge
lerc                      4.0.0                h27087fc_0    conda-forge
libblas                   3.9.0           16_linux64_openblas    conda-forge
libbrotlicommon           1.0.9                h166bdaf_7    conda-forge
libbrotlidec              1.0.9                h166bdaf_7    conda-forge
libbrotlienc              1.0.9                h166bdaf_7    conda-forge
libcblas                  3.9.0           16_linux64_openblas    conda-forge
libcurl                   7.85.0               h2283fc2_0    conda-forge
libdeflate                1.14                 h166bdaf_0    conda-forge
libedit                   3.1.20191231         he28a2e2_2    conda-forge
libev                     4.33                 h516909a_1    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 12.1.0              h8d9b700_16    conda-forge
libgd                     2.3.3                h18fbbfe_3    conda-forge
libgfortran-ng            12.1.0              h69a702a_16    conda-forge
libgfortran5              12.1.0              hdcd56e2_16    conda-forge
libglib                   2.74.0               h7a41b64_0    conda-forge
libgomp                   12.1.0              h8d9b700_16    conda-forge
libiconv                  1.17                 h166bdaf_0    conda-forge
liblapack                 3.9.0           16_linux64_openblas    conda-forge
libnghttp2                1.47.0               hff17c54_1    conda-forge
libnsl                    2.0.0                h7f98852_0    conda-forge
libopenblas               0.3.21          pthreads_h78a6416_3    conda-forge
libpng                    1.6.38               h753d276_0    conda-forge
librsvg                   2.54.4               h7abd40a_0    conda-forge
libsqlite                 3.39.3               h753d276_0    conda-forge
libssh2                   1.10.0               hf14f497_3    conda-forge
libstdcxx-ng              12.1.0              ha89aaad_16    conda-forge
libtiff                   4.4.0                h55922b4_4    conda-forge
libtool                   2.4.6             h9c3ff4c_1008    conda-forge
libuuid                   2.32.1            h7f98852_1000    conda-forge
libwebp                   1.2.4                h522a892_0    conda-forge
libwebp-base              1.2.4                h166bdaf_0    conda-forge
libxcb                    1.13              h7f98852_1004    conda-forge
libxml2                   2.10.2               h4c7fe37_1    conda-forge
libxslt                   1.1.35               h8affb1d_0    conda-forge
libzlib                   1.2.12               h166bdaf_3    conda-forge
looseversion              1.0.1              pyhd8ed1ab_0    conda-forge
lxml                      4.9.1            py39hb9d737c_0    conda-forge
matplotlib-base           3.6.0            py39hf9fd14e_0    conda-forge
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
ncurses                   6.3                  h27087fc_1    conda-forge
networkx                  2.8.6              pyhd8ed1ab_0    conda-forge
nibabel                   4.0.2              pyhd8ed1ab_0    conda-forge
nilearn                   0.10.0                   pypi_0    pypi
nipype                    1.8.5            py39hf3d152e_1    conda-forge
numpy                     1.20.3           py39hdbf815f_0    conda-forge
openjpeg                  2.5.0                h7d73246_1    conda-forge
openssl                   3.0.7                h166bdaf_0    conda-forge
packaging                 21.3               pyhd8ed1ab_0    conda-forge
pandas                    1.5.1            py39h4661b88_1    conda-forge
pango                     1.50.10              hc4f8a73_0    conda-forge
pathlib                   1.0.1            py39hf3d152e_7    conda-forge
pcre2                     10.37                hc3806b6_1    conda-forge
pillow                    9.2.0            py39hd5dbb17_2    conda-forge
pip                       22.2.2             pyhd8ed1ab_0    conda-forge
pixman                    0.40.0               h36c2ea0_0    conda-forge
prov                      2.0.0              pyhd3deb0d_0    conda-forge
psutil                    5.9.2            py39hb9d737c_0    conda-forge
pthread-stubs             0.4               h36c2ea0_1001    conda-forge
pycparser                 2.21               pyhd8ed1ab_0    conda-forge
pydicom                   2.3.0              pyh6c4a22f_0    conda-forge
pydot                     1.4.2            py39hf3d152e_2    conda-forge
pyopenssl                 22.0.0             pyhd8ed1ab_1    conda-forge
pyparsing                 3.0.9              pyhd8ed1ab_0    conda-forge
pyproject-toml            0.0.10                   pypi_0    pypi
pyrsistent                0.19.3                   pypi_0    pypi
pysocks                   1.7.1            py39hf3d152e_5    conda-forge
python                    3.9.13          h2660328_0_cpython    conda-forge
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python_abi                3.9                      2_cp39    conda-forge
pytz                      2022.6             pyhd8ed1ab_0    conda-forge
rdflib                    6.2.0              pyhd8ed1ab_0    conda-forge
readline                  8.1.2                h0f457ee_0    conda-forge
requests                  2.28.1             pyhd8ed1ab_1    conda-forge
scikit-learn              1.1.3            py39hd5c8da3_1    conda-forge
scipy                     1.9.1            py39h8ba3f38_0    conda-forge
setuptools                65.4.0             pyhd8ed1ab_0    conda-forge
simplejson                3.17.6           py39hb9d737c_1    conda-forge
six                       1.16.0             pyh6c4a22f_0    conda-forge
sqlite                    3.39.3               h4ff8645_0    conda-forge
threadpoolctl             3.1.0              pyh8a188c0_0    conda-forge
tk                        8.6.12               h27826a3_0    conda-forge
toml                      0.10.2                   pypi_0    pypi
traits                    6.4.1            py39hb9d737c_0    conda-forge
tzdata                    2022d                h191b570_0    conda-forge
unicodedata2              14.0.0           py39hb9d737c_1    conda-forge
urllib3                   1.26.11            pyhd8ed1ab_0    conda-forge
webencodings              0.5.1                      py_1    conda-forge
wheel                     0.37.1             pyhd8ed1ab_0    conda-forge
xorg-kbproto              1.0.7             h7f98852_1002    conda-forge
xorg-libice               1.0.10               h7f98852_0    conda-forge
xorg-libsm                1.2.3             hd9c2040_1000    conda-forge
xorg-libx11               1.7.2                h7f98852_0    conda-forge
xorg-libxau               1.0.9                h7f98852_0    conda-forge
xorg-libxdmcp             1.1.3                h7f98852_0    conda-forge
xorg-libxext              1.3.4                h7f98852_1    conda-forge
xorg-libxrender           0.9.10            h7f98852_1003    conda-forge
xorg-renderproto          0.11.1            h7f98852_1002    conda-forge
xorg-xextproto            7.3.0             h7f98852_1002    conda-forge
xorg-xproto               7.0.31            h7f98852_1007    conda-forge
xvfbwrapper               0.2.9           pyhd8ed1ab_1005    conda-forge
xz                        5.2.6                h166bdaf_0    conda-forge
zipp                      3.8.1              pyhd8ed1ab_0    conda-forge
zlib                      1.2.12               h166bdaf_3    conda-forge
zstd                      1.5.2                h6239696_4    conda-forge

How can I solve this problem? :face_with_spiral_eyes:
Thanks a lot! :heartpulse:

Hi @zhouyu0206 and welcome to neurostars!

Hmm, I was unable to replicate this on my end. from nilearn.decoding import Decoder worked for me. Do other nilearn import functions work? E.g., can you just run import nilearn?

How are you accessing python? E.g., your terminal, some IDE (e.g., pycharm), or jupyter lab/notebook, etc…

Best,
Steven

1 Like

Hi @Steven and thank you so much for your reply! :grinning:

I can run import nilearn, it works well except a warning message:

/opt/software/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

I’m doing the computation in a remote computing center and access python from ipython, which looks like this:

(mri) [guixue@cu72 ~]$ ipython
[TerminalIPythonApp] ERROR | Failed to open SQLite history /brain/guixue/.ipython/profile_default/history.sqlite (disk I/O error).
[TerminalIPythonApp] ERROR | History file was moved to /brain/guixue/.ipython/profile_default/history-corrupt.sqlite and a new file created.
[TerminalIPythonApp] ERROR | Failed to open SQLite history /brain/guixue/.ipython/profile_default/history.sqlite (disk I/O error).
[TerminalIPythonApp] ERROR | History file was moved to /brain/guixue/.ipython/profile_default/history-corrupt.sqlite and a new file created.
[TerminalIPythonApp] ERROR | Failed to open SQLite history /brain/guixue/.ipython/profile_default/history.sqlite (disk I/O error).
[TerminalIPythonApp] ERROR | Failed to load history too many times, history will not be saved.
Python 3.6.6 |Anaconda 4.4.0 (64-bit)| (default, Oct  9 2018, 12:34:16) 
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import nilearn
/opt/software/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

In [2]: import nilearn.decoding
/brain/guixue/.local/lib/python3.6/site-packages/sklearn/utils/deprecation.py:143: FutureWarning: The sklearn.linear_model.base module is  deprecated in version 0.22 and will be removed in version 0.24. The corresponding classes / functions should instead be imported from sklearn.linear_model. Anything that cannot be imported from sklearn.linear_model is now part of the private API.
  warnings.warn(message, FutureWarning)

In [3]: from nilearn.decoding import Decoder
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-3-85912a8da56d> in <module>()
----> 1 from nilearn.decoding import Decoder

ImportError: cannot import name 'Decoder'

Best,
Yu

It looks to me like there is something wrong with the environment. In the list of packages you provide above, it says you are supposed to be using python3.9.13, but the import warning seems to suggest it is using another python3.6 installation. Maybe try to remove your environment and set up a new one, to see if that fixes it. Alternatively you can try what happens if you don’t use conda, but python’s venv utility. When I used conda, I had similar problems to these in the past. Of course the advantage of conda is that you can install python versions other than the system-wide installation.

1 Like

Thanks a lot! :grinning: I think you’re right! I reinstall the nilearn by conda install -c conda-forge nilearn instead of pip, then it worked. Now I can import Decoder:

(mri) [guixue@cu72 pbs_stats_5freqs]$ ipython
[TerminalIPythonApp] ERROR | Failed to open SQLite history /brain/guixue/.ipython/profile_default/history.sqlite (disk I/O error).
[TerminalIPythonApp] ERROR | History file was moved to /brain/guixue/.ipython/profile_default/history-corrupt.sqlite and a new file created.
[TerminalIPythonApp] ERROR | Failed to open SQLite history /brain/guixue/.ipython/profile_default/history.sqlite (disk I/O error).
[TerminalIPythonApp] ERROR | History file was moved to /brain/guixue/.ipython/profile_default/history-corrupt.sqlite and a new file created.
[TerminalIPythonApp] ERROR | Failed to open SQLite history /brain/guixue/.ipython/profile_default/history.sqlite (disk I/O error).
[TerminalIPythonApp] ERROR | Failed to load history too many times, history will not be saved.
Python 3.9.13 | packaged by conda-forge | (main, May 27 2022, 16:58:50) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import nilearn

In [2]: from nilearn.decoding import Decoder

In [3]: 

Thanks again :grin: :heartpulse:

Best,
Yu

1 Like