Make AFNI compatible with both Python 2 and 3

Dear AFNI team,

Recently, I realize that using AFNI by writing ipython script (*.ipy) is highly enjoyable compared with tcsh or bash scripts,
except for the Python 2/3 compatibility issue.

Although Python 2 and 3 have been running in parallel for a long time, but currently I feel the migrating speed toward Python 3 is accelerating.
For example,

  1. Python 2.7 stops to get feature back port.
  2. Most popular packages have already been 2/3 compatible.
  3. Some major packages (e.g., IPython) start to maintain Python 3 only code base.
    I personally find Python 3 quite convenient to work with, given the accumulating new language features.
    So I have Python 3 installed on my computer as the default Python interpreter.

Unfortunately, the Python-related code in AFNI only works with Python 2.
In order to use AFNI smoothly, I have to switch to Python 2 manually each time.
In fact, it is usually quit easy to refactor the code to make it compatible with both Python 2 and 3.
For example, one prominent issue that currently prevents the scripts from running with Python 3 is the ubiquitous
print “some message”
To be 2/3 compatible, simply change it into
print(“some message”)
For more involved cases, there is a single file package called “six” which provides a compatibility layer.
There is also tools to automate such translations.

Will you consider refactoring the python code to also support Python 3?
It will be truly appreciated and I will also be happy if there is anything I can help~

Thanks,

Thanks for the interest. That effort is on-going and, I believe, almost complete.

Wow, that is great!

Thanks for all your efforts!

It seems we’re still in Python 2 land. One alternate is to just change the start of each script from:

/usr/bin/env python
to
/usr/bin/env python2.7

I just tried this on a current Mac and several Linux boxes without issue (Macs don’t have python2 as an option by default). This way, though, the scripts get to stay Python2 and yet those of us with default Python3 environments don’t have to juggle things around just to run the scripts.

Craig

Chiming in here because this seems to have died without a resolution…

Going into abin, listing all the .py files, and running 2to3 -w on all of them seems to have resulted in a version of AFNI that works with Python 3.6 and 2.7 (well, it starts, certainly, which it didn’t do before, although I haven’t tested it extensively). As far as I can tell from perusing the output of 2to3, the only thing it really needed to change was print statements and excepts, which can be converted automatically with no problem.

Has anybody tried this and found it NOT to work? If there are problem files, I’m certainly happy to look at them to try to hand convert them.

Blaise

We’re pretty aware of this. And we did something similar. For what it’s worth, both align_epi_anat.py and afni.proc.py and their dependencies should already work in python 3. We’re still waiting for someone, who shall remain nameless, to go through the tiny effort to make this all compatible with either python 2 or 3 versions.

Daniel-

Agreed. Certainly, if I see any such people who need to get their act together, I will let them know!!

-pt

Ah, I was hoping some people or person would be watching the messageboard in Seattle in between bites of salmon. Salmon and FMRI go together.