module 'importlib' has no attribute 'find_spec'

I think I correctly installed mrtrix3 with anaconda on my ubuntu server. I did it both in a envirnomnet with python version 3.12.4 and in another environment with python 2.7.18

When I do 5ttgen in the python3 environment I receive:

(base) mariacabello@neuroworki:~/Desktop$ 5ttgen
Traceback (most recent call last):
  File "/home/mariacabello/anaconda3/bin/5ttgen", line 69, in <module>
    import mrtrix3
  File "/home/mariacabello/anaconda3/bin/mrtrix3.py", line 38, in <module>
    if not imported (os.path.normpath (os.path.join ( \
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mariacabello/anaconda3/bin/mrtrix3.py", line 25, in imported
    fp, pathname, description = imp.find_spec('mrtrix3', [ lib_path ])
                                ^^^^^^^^^^^^^
AttributeError: module 'importlib' has no attribute 'find_spec'

When I do 5ttgen in the python2 environment I receive:

(py27) mariacabello@neuroworki:~/Desktop$ 5ttgen
Traceback (most recent call last):
  File "/home/mariacabello/anaconda3/envs/py27/bin/5ttgen", line 69, in <module>
    import mrtrix3
  File "/home/mariacabello/anaconda3/envs/py27/bin/mrtrix3.py", line 39, in <module>
    os.path.dirname (os.path.realpath (__file__)), os.pardir, 'lib') )):
  File "/home/mariacabello/anaconda3/envs/py27/bin/mrtrix3.py", line 25, in imported
    fp, pathname, description = imp.find_spec('mrtrix3', [ lib_path ])
AttributeError: 'module' object has no attribute 'find_spec'

Please help

Hi Maria,

The Python 3.12 update seems to have broken a couple of things. Chaning your environment to 3.11 should resolve.
I’m a little surprised that Python2 doesn’t work, given we’ve invested efforts to this point in preserving compatibility. But it is now officially deprecated, and we will be dropping Python2 support in MRtrix 3.1.

Rob

Hi Rob,

Thanks for your answer.

I did:

conda create -n "python311" python=3.11 ipython
conda activate python311
conda install -c mrtrix3 mrtrix3
5ttgen

and I am still getting this for the last line:

(python311) mariacabello@neuroworki:~/Desktop$ 5ttgen
Traceback (most recent call last):
  File "/home/mariacabello/anaconda3/envs/python311/bin/5ttgen", line 69, in <module>
    import mrtrix3
  File "/home/mariacabello/anaconda3/envs/python311/bin/mrtrix3.py", line 38, in <module>
    if not imported (os.path.normpath (os.path.join ( \
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mariacabello/anaconda3/envs/python311/bin/mrtrix3.py", line 25, in imported
    fp, pathname, description = imp.find_spec('mrtrix3', [ lib_path ])
                                ^^^^^^^^^^^^^
AttributeError: module 'importlib' has no attribute 'find_spec'

imp was removed in 3.12, but still present in 3.11.

From this part of the traceback:

  File "/home/mariacabello/anaconda3/envs/python311/bin/mrtrix3.py", line 25, in imported
    fp, pathname, description = imp.find_spec('mrtrix3', [ lib_path ])
                                ^^^^^^^^^^^^^

, not only is it reporting an absence of that function in the importlib rather than imp module, but that doesn’t correspond to the source code at the latest tag. Are you attempting to install a version of MRtrix3 other than the latest one, and have you performed any local code edits?

Notably, this code has been modified on master via #2735 due to prior flagging of imp deprecation in #2731, but this has not yet made its way to a tag update. So if we were to create a 3.0.5 it might resolve at least this part; but there are other Python 3.12 issues not yet resolved.