ImportError: cannot import name 'gcd' from 'fractions'

GROMACS version:
GROMACS modification: Yes/No
Hello everyone, while doing protein ligand complex-ligand topology building step I am getting output stating that cannot import name 'gcd:. I am using python 3.10.6 with script cgenff_charmm2gmx_py3_nx2.py with networkx-2.3 and I am getting the below output:
/gromacs/test1$ python3 cgenff_charmm2gmx.py UNK unk_fix.mol2 unk.str charmm36-jul2021.ff
Traceback (most recent call last):
File “/mnt/c/users/admin/desktop/gromacs/test1/cgenff_charmm2gmx.py”, line 54, in
import networkx as nx
File “/home/shanu/.local/lib/python3.10/site-packages/networkx/init.py”, line 114, in
import networkx.generators
File “/home/shanu/.local/lib/python3.10/site-packages/networkx/generators/init.py”, line 14, in
from networkx.generators.intersection import *
File “/home/shanu/.local/lib/python3.10/site-packages/networkx/generators/intersection.py”, line 13, in
from networkx.algorithms import bipartite
File “/home/shanu/.local/lib/python3.10/site-packages/networkx/algorithms/init.py”, line 16, in
from networkx.algorithms.dag import *
File “/home/shanu/.local/lib/python3.10/site-packages/networkx/algorithms/dag.py”, line 23, in
from fractions import gcd
ImportError: cannot import name ‘gcd’ from ‘fractions’ (/usr/lib/python3.10/fractions.py)

Also, when I changed the networkx 2.5, I am getting below output:
/gromacs/test1$ python3 cgenff_charmm2gmx.py UNK unk_fix.mol2 unk.str charmm36-jul2021.ff
This script has been tested with NetworkX 2.3, and 2.4 is buggy.
Please install version 2.3 for best performance:
pip uninstall networkx
pip install networkx==2.3
NOTE 1: Code tested with Python 3.5.2 and 3.7.3. Your version: 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0]

NOTE 2: Code tested with NetworkX 2.3. Your version: 2.5

NOTE 3: Please be sure to use the same version of CGenFF in your simulations that was used during parameter generation:
–Version of CGenFF detected in unk.str : 4.6
–Version of CGenFF detected in charmm36-jul2021.ff/forcefield.doc : 4.6

NOTE 4: To avoid duplicated parameters, do NOT select the ‘Include parameters that are already in CGenFF’ option when uploading a molecule into CGenFF.
Traceback (most recent call last):
File “/mnt/c/users/admin/desktop/gromacs/test1/cgenff_charmm2gmx.py”, line 1044, in
m.read_mol2_coor_only(mol2_name)
File “/mnt/c/users/admin/desktop/gromacs/test1/cgenff_charmm2gmx.py”, line 904, in read_mol2_coor_only
self.G.node[atomi][‘x’] = float(entry[2])
AttributeError: ‘Graph’ object has no attribute ‘node’. Did you mean: ‘_node’?

Please help me out with that.
Thank you

The script uses slightly older syntax, which needs to be updated. You can easily fix it with the advice here: python - Import Error: can't import name gcd from fractions - Stack Overflow

If you have further questions about this, please email me. This is not a GROMACS problem.

The “G.node” accessor was deprecated in 2.4
(https://networkx.org/documentation/stable/release/release_2.4.html#deprecations), you need to change line 904 in read_mol2_coor_only to “G.nodes”.

Kindly tell me how did you rectify this error as I’m facing the same problem as well.

Either use an older Python version (e.g. 3.7) or you will have to update the code. The syntax we use became deprecated some time ago and I haven’t had a chance to fix it and test but there are solutions posted online with a simple search.

Did you find any solution that works?

Is there a solution this issue? We are facing the same issue and not able to rectify. Any suggestion is highly appreciable.
Thanks in advance.

Did you try creating a virtual environment for python 3.7, then running gromacs commands from inside it? Stackoverflow got a thread discussing exactly this.

Hello,

I am currently trying to follow the protein-ligand complex tutorial online but have run into an error with converting a the CHARMM jz4.str file into GROMACS input files. I have been trying to follow the discussion in this forum ImportError: cannot import name 'gcd' from 'fractions' and tried following your suggestion to look here python - Import Error: can't import name gcd from fractions - Stack Overflow. I am using python 3.12 and have tried using a few versions of networkx including 1.11, 2.5, and 3.3. The error persists. This error I have attached was from using the 1.11 version. I am using GROMACS2020.

I would greatly appreciate help solving this issue. It would probably be easier to edit the code to satisfy the newer version of python than to downgrade. Is it possible for you to instruct me on how to do this? I cannot figure out what to edit according to the website you suggested on the forum where it says this “fractions.gcd(a, b) has been moved to the math module in Python 3.9. So just replace the call with math.gcd(a, b).” I see no place to make that replacement.

The line I am trying to run and error are below. I greatly would appreciate any assistance.

The line I am trying to run is
python3 cgenff_charmm2gmx.py JZ4 jz4_fix.mol2 jz4.str charmm36-jul2022.ff

I receive this error:
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:60: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:67: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:130: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:141: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:166: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:183: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:579: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:583: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:598: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.rstrip(string.lstrip(line)))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:608: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.rstrip(string.lstrip(line)))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:632: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:856: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py:883: SyntaxWarning: invalid escape sequence ‘\s’
entry = re.split(‘\s+’, string.lstrip(line))
Traceback (most recent call last):
File “/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx.py”, line 51, in
import networkx as nx
File “/home/bioreasearchlab/miniconda3/envs/networkx/lib/python3.12/site-packages/networkx/init.py”, line 114, in
import networkx.generators
File “/home/bioreasearchlab/miniconda3/envs/networkx/lib/python3.12/site-packages/networkx/generators/init.py”, line 6, in
from networkx.generators.classic import *
File “/home/bioreasearchlab/miniconda3/envs/networkx/lib/python3.12/site-packages/networkx/generators/classic.py”, line 26, in
from networkx.algorithms.bipartite.generators import complete_bipartite_graph
File “/home/bioreasearchlab/miniconda3/envs/networkx/lib/python3.12/site-packages/networkx/algorithms/init.py”, line 16, in
from networkx.algorithms.dag import *
File “/home/bioreasearchlab/miniconda3/envs/networkx/lib/python3.12/site-packages/networkx/algorithms/dag.py”, line 14, in
from fractions import gcd
ImportError: cannot import name ‘gcd’ from ‘fractions’ (/home/bioreasearchlab/miniconda3/envs/networkx/lib/python3.12/fractions.py)

Update:

I realized that I was not using the correct cgenff_charmm2gmx.py file. I now tried using the following command after downloading cgenff_charmm2gmx_py3_nx1.py:

python3 cgenff_charmm2gmx_py3_nx1.py JZ4 jz4_fix.mol2 jz4.str charmm36-jul2022.ff

I had tried to download python 3.7.17 to make it run without issue, as I read on the github that versions above 3.7 don’t work. Is it possible that that is too new of a version? When I input

python3.7 --version

it returns:

Python 3.7.17

indicating it is downloaded, however, when I try the input line: python3 cgenff_charmm2gmx_py3_nx1.py JZ4 jz4_fix.mol2 jz4.str charmm36-jul2022.ff

I still receive the fractions import gcd error below:

File “/home/bioreasearchlab/gromacs-2020/build/Ligand_Test/cgenff_charmm2gmx_py3_nx1.py”, line 51, in
import networkx as nx
File “/home/bioreasearchlab/.local/lib/python3.10/site-packages/networkx/init.py”, line 114, in
import networkx.generators
File “/home/bioreasearchlab/.local/lib/python3.10/site-packages/networkx/generators/init.py”, line 14, in
from networkx.generators.intersection import *
File “/home/bioreasearchlab/.local/lib/python3.10/site-packages/networkx/generators/intersection.py”, line 13, in
from networkx.algorithms import bipartite
File “/home/bioreasearchlab/.local/lib/python3.10/site-packages/networkx/algorithms/init.py”, line 16, in
from networkx.algorithms.dag import *
File “/home/bioreasearchlab/.local/lib/python3.10/site-packages/networkx/algorithms/dag.py”, line 23, in
from fractions import gcd
ImportError: cannot import name ‘gcd’ from ‘fractions’ (/home/bioreasearchlab/miniconda3/lib/python3.10/fractions.py)

It seems as though the linux server is still using python3.10 instead of the 3.7.17. Would you be able to assist me in making it use the 3.7.17 version instead?

Thank you again.