Pymatgen structure matcher import error

Hello, I am trying to run the following code:

from os.path import isfile
from pymatgen import Structure
from pymatgen.analysis.structure_matcher import StructureMatcher, ElementComparator
from pymatgen.io.cifio import CifWriter, CifParser

from mpworks.snl_utils.snl_mongo import SNLMongoAdapter
sma = SNLMongoAdapter.auto_load()


def get_structure(gid):
    ciffile = 'snlgroup_id_%d.cif' % gid
    if not isfile(ciffile):
        s = Structure.from_dict(sma.snlgroups.find_one(
            {"snlgroup_id": gid})['canonical_snl'])
        w = CifWriter(s)
        w.write_file(ciffile)
    else:
        s = CifParser(ciffile).get_structures()[0]
    return s


# two sets of snlgroup_id pairs: a couple of canonical structures from the
# A. ~405 that didn't match a long time ago but now should match
# B. ~3500 that seemed like they matched a few months ago, but we realize now it was due to a bug
# duplicate check needs to return True for A. and False for B.!
setA = [
    [124070, 124069], [88026, 88025], [33468, 33466],
    [150493, 150494], [31564, 31563], [87801, 87800]
]
setB = [
    [5335, 5336], [123806, 123805], [90125, 90124],
    [18884, 18883], [129197, 129196], [164662, 164639],
]

sm = StructureMatcher(
    ltol=0.2, stol=0.3, angle_tol=5, primitive_cell=True, scale=True,
    attempt_supercell=False, comparator=ElementComparator()
)

for list_of_gid_pairs in [setA, setB]:
    print(list_of_gid_pairs)
    for gid_pair in list_of_gid_pairs:
        s1, s2 = [get_structure(gid) for gid in gid_pair]
        print(gid_pair, sm.fit(s1, s2))

I have installed pymatgen using pip install pymatgen, and have also tried pip install -U pymatgen to ensure that I have the newest version, which I do. When I try to import the “get_structure” function in an ipynb file, I get this error:
ImportError Traceback (most recent call last)
Cell In[1], line 1
----> 1 from pymatgen_matcher import get_structure

File ~/Desktop/crystal_similarity/pymatgen_matcher.py:2
1 from os.path import isfile
----> 2 from pymatgen import Structure
3 from pymatgen.analysis.structure_matcher import StructureMatcher, ElementComparator
4 from pymatgen.io.cifio import CifWriter, CifParser

ImportError: cannot import name ‘Structure’ from ‘pymatgen’ (unknown location)

Any reason why it can’t find the pymatgen location?

hey @willcoomans5, welcome to matsci!

The code snippet you posted contains a root-level import of Structure, which was removed a long time ago. You should instead be importing Structure from pymatgen.core.structure. As far as I can see, this snippet contains other legacy imports, which will result in similar errors. For instance, pymatgen.io.cifio no longer exists, and it should instead read pymatgen.io.cif. For the pymatgen related imports these are the ones I can see, and as for the rest, if something else fails with an ImportError my suggestion would be to go to the documentation of the respective package to fix the imports, as this is one of the most common headaches when it comes to running old code.

Hope this helps!