Python problem with pymatgen

I’ve installed pymatgen in a fresh docker. I can run a jupyter notebook in that docker. Most of pymatgen works, but there are some weird problems that some parts of pymatgen are not visible, or can’t be imported. The notebook ran for the student that created in, so it must by some sort of python-related thing.

This doesn’t work:

ImportError                               Traceback (most recent call last)
<ipython-input-20-2965e9c89935> in <module>
      1 from ase.db import connect  # api for connecting to the atoms database
      2 import pymatgen.analysis.phase_diagram as PD
----> 3 from pymatgen import Element
      4 #

ImportError: cannot import name 'Element' from 'pymatgen' (unknown location)

but this does:

import pymatgen

And this function does not seem to exist, but worked for my student:

AttributeError                            Traceback (most recent call last)
<ipython-input-13-46de504dce8b> in <module>
----> 1 stables = pd.get_stable_entries_normed()
      3 for struct in stables:
      4     print(struct.attribute)

AttributeError: 'PhaseDiagram' object has no attribute 'get_stable_entries_normed'

Hi Gus,

There was a breaking change made in v2022 onwards. You can pin to v2021 to retain the existing imports.

In short, the convenience imports were removed to allow us to switch to a namespace package, which will make pymatgen more modular, and ultimately allow for a more focused and streamlined pymatgen core (with fewer dependencies) while still allowing for the more complex functionality people want.

The announcement is on the pymatgen website: Introduction — pymatgen 2022.0.4 documentation

But in short, these are the convenience imports removed in favour of their canonical imports:

from pymatgen import Composition -> now "from pymatgen.core.composition import Composition"
from pymatgen import Lattice -> now "from pymatgen.core.lattice import Lattice"
from pymatgen import SymmOp -> now "from pymatgen.core.operations import SymmOp"
from pymatgen import DummySpecie, DummySpecies, Element, Specie, Species -> now "from pymatgen.core.periodic_table ..."
from pymatgen import PeriodicSite, Site -> now "from pymatgen.core.sites ..."
from pymatgen import IMolecule, IStructure, Molecule, Structure -> now "from pymatgen.core.structure ..."
from pymatgen import ArrayWithUnit, FloatWithUnit, Unit -> now "from pymatgen.core.units ..."
from pymatgen import Orbital, Spin -> now "from pymatgen.electronic_structure.core ..."
from pymatgen import MPRester -> now "from pymatgen.ext.matproj ..."

I believe from pymatgen.core import Element would still work too.

As you can imagine, such a change has caused some extra work for packages downstream, which is why it’s been given the v2022 version to allow people to continue using the v2021 for now.




Thank you, Matt! That was a helpful response and timely. Rather than pinning v2021, I’m just updating my notebook to run with the latest version.

I was running fresh in a new docker so I re-installed everything and hadn’t read the docks about new changes. I figured my student was using the latest already—apparently I was wrong. Sorry for the trouble.

Do you have a comment on the second error that I listed? That doesn’t seem to be related to the v2022 breaking changes (AFAICT), but I can’t get around that, even after seeing the v2022 changes…

I reinstalled: python -m pip install pymatgen==v2021.3.3 --force-reinstall but I’m still getting that second error…

mtp_db_ID = [struct.attribute for struct in pd.get_stable_entries_normed()]
AttributeError                            Traceback (most recent call last)
<ipython-input-11-1818f4f9b8bd> in <module>
----> 1 mtp_db_ID = [struct.attribute for struct in pd.get_stable_entries_normed()]

AttributeError: 'PhaseDiagram' object has no attribute 'get_stable_entries_normed'

Hi @glwhart, the get_stable_entries_normed() method was removed in pymatgen v2021.3.3.

You should be able to get stable entries from the phase diagram with pd.stable_entries. If normalized entries are needed, the normalize() method can be called on each of the entries to return a new (normalized) entry – these two steps are what the previous method was doing actually!

1 Like

Thank you Matt! Very helpful. I just picked up a notebook from a student. I didn’t anticipate the breaking changes. I’m a bit rusty…

1 Like

Big thank you’s to both @mkhorton and @mattmcdermott. I’m finally making plots with my student’s notebook.


Thank you Matt. It (pymatgen.core import Element) works. Can you let me know how to pin to v2021. or Is there a need to retain the existing imports?

If you can update your code to do so, I would just use from pymatgen.core import ... instead. The functionality is equivalent, and pre-2022 versions also support this syntax, so code written in this way will work with both older and newer versions of pymatgen.

However, if updating your code isn’t possible, you can pin to a specific version in the usual way, which depends on what Python system you’re using, e.g.

pip install pymatgen==2021.2.16


conda install pymatgen=2021.2.16

or putting an appropriate line in your requirements.txt or

Hope this helps,


1 Like

Great ! thank you. it makes sense.