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

**URL:** <https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782>\
**Category:** User discussions\
**Tags:** forcefield\
**Created:** [February 11, 2023, 5:00pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782 "2023-02-11T17:00:18Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sandeep](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sandeep](https://gromacs.bioexcel.eu/u/sandeep)\
**Post date:** [February 11, 2023, 5:00pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/1 "2023-02-11T17:00:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jalemkul](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/jalemkul/32/18_2.png) [@jalemkul](https://gromacs.bioexcel.eu/u/jalemkul)\
**Post date:** [February 16, 2023, 2:31pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/2 "2023-02-16T14:31:12Z")

</div>

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](https://stackoverflow.com/questions/66174862/import-error-cant-import-name-gcd-from-fractions)

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

---

<div class="post-metadata">

**Author:** ![tbaba](https://avatars.discourse-cdn.com/v4/letter/t/838e76/32.png) [@tbaba](https://gromacs.bioexcel.eu/u/tbaba)\
**Post date:** [March 6, 2023, 12:46pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/3 "2023-03-06T12:46:02Z")

</div>

The “G.node” accessor was deprecated in 2.4  
([https://networkx.org/documentation/stable/release/release\_2.4.html#deprecations](https://www.researchgate.net/deref/https%3A%2F%2Fnetworkx.org%2Fdocumentation%2Fstable%2Frelease%2Frelease_2.4.html%23deprecations)), you need to change line 904 in read\_mol2\_coor\_only to “G.nodes”.

---

<div class="post-metadata">

**Author:** ![tan2](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@tan2](https://gromacs.bioexcel.eu/u/tan2)\
**Post date:** [June 28, 2023, 4:45am UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/4 "2023-06-28T04:45:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jalemkul](https://dub1.discourse-cdn.com/flex017/user_avatar/gromacs.bioexcel.eu/jalemkul/32/18_2.png) [@jalemkul](https://gromacs.bioexcel.eu/u/jalemkul)\
**Post date:** [June 28, 2023, 5:55am UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/5 "2023-06-28T05:55:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sharma-lakshya](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@sharma-lakshya](https://gromacs.bioexcel.eu/u/sharma-lakshya)\
**Post date:** [October 19, 2023, 12:12pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/6 "2023-10-19T12:12:38Z")

</div>

Did you find any solution that works?

---

<div class="post-metadata">

**Author:** ![mannu](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mannu](https://gromacs.bioexcel.eu/u/mannu)\
**Post date:** [March 11, 2024, 12:13pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/7 "2024-03-11T12:13:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TKofUiB](https://avatars.discourse-cdn.com/v4/letter/t/54ee81/32.png) [@TKofUiB](https://gromacs.bioexcel.eu/u/TKofUiB)\
**Post date:** [March 12, 2024, 11:51am UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/8 "2024-03-12T11:51:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![isilver](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@isilver](https://gromacs.bioexcel.eu/u/isilver)\
**Post date:** [July 8, 2024, 9:24pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/9 "2024-07-08T21:24:02Z")

</div>

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'](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782) and tried following your suggestion to look here [python - Import Error: can't import name gcd from fractions - Stack Overflow](https://stackoverflow.com/questions/66174862/import-error-cant-import-name-gcd-from-fractions). 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)

---

<div class="post-metadata">

**Author:** ![isilver](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@isilver](https://gromacs.bioexcel.eu/u/isilver)\
**Post date:** [July 8, 2024, 9:42pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/10 "2024-07-08T21:42:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![slaa1999](https://avatars.discourse-cdn.com/v4/letter/s/a9a28c/32.png) [@slaa1999](https://gromacs.bioexcel.eu/u/slaa1999)\
**Post date:** [August 7, 2025, 9:07pm UTC](https://gromacs.bioexcel.eu/t/importerror-cannot-import-name-gcd-from-fractions/5782/11 "2025-08-07T21:07:38Z")

</div>

Hello,

Your solution is a fingertip away, to be more precise, the last line of your error output is where the problem and solution can be found.

As stated by @jalemkul reply, `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)`.

Edit the dag.py, line 14 to the following:

from fractions import gcd— change this to— **from math import gcd.**

Hopefully this helps.
