Why it shows module 'nilearn' has no attribute 'plotting'?

192 views Asked by At

When I run

nilearn.plotting.plot_connectome()

it shows me that module 'nilearn' has no attribute 'plotting', how is it possible? I have reinstalled the nilearn library many times and ran the code on a different computer, but it didn't work.
Does anyone know the reason and how to solve this error? Thank you for any help!

3

There are 3 answers

0
DoneForAiur On

Instead of import nilearn, you should import it as import nilearn.plotting.

0
Shiva On

nilearn does not automatically import the submodules such as plotting. This is usually done to save memory and/or improve performance since there could be hundreds of submodules or each submodule could be very large or the submodule is not frequently used.

There could be other reasons and the decision to automatically import submodules ultimately depend on the library's author(s) which in turn could be based on the usage patterns of the library.

As the submodule plotting is not automatically imported you have to import it explicitly. This can be done in two ways:

  • import nilearn
    import nilearn.plotting
    
  • import nilearn
    from nilearn import plotting
    

Also, plot_connectome requires at least two arguments - 'adjacency_matrix' and 'node_coords' which I don't see in your code.

0
Shawn On

It's very curious. Yesterday it didn't work. But today it works after I retried. Now on my computer,

from nilearn import plotting

works.