Repository: materialsproject/pymatgen

Issue #3644: Can no longer import some modules without having ASE installed
Created: 2024-02-22T14:04:10Z
Python version
3.11.7
Pymatgen version
2024.2.20
Operating system version
Any
Current behavior
I observed this in a fresh virtual environment when trying to install and use mp_api. Before #3619 (or related follow-up commits, can't tell) you were able to import the AseAtomsAdaptor without an error, even if you didn't have ASE installed. This was changed by the creation of the MSONAtoms class which uses Atoms via inheritance and thus is loaded at import time.
Example as posted in #3619 (comment)
>>> from mp_api.client.mprester import MPRester
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/mp_api/client/__init__.py", line 8, in <module>
    from .mprester import MPRester
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/mp_api/client/mprester.py", line 10, in <module>
    from emmet.core.electronic_structure import BSPathType
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/emmet/core/electronic_structure.py", line 11, in <module>
    from pymatgen.analysis.magnetism.analyzer import (
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/pymatgen/analysis/magnetism/__init__.py", line 5, in <module>
    from pymatgen.analysis.magnetism.analyzer import (
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/pymatgen/analysis/magnetism/analyzer.py", line 24, in <module>
    from pymatgen.transformations.advanced_transformations import MagOrderingTransformation, MagOrderParameterConstraint
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/pymatgen/transformations/advanced_transformations.py", line 33, in <module>
    from pymatgen.io.ase import AseAtomsAdaptor
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-mp-api/lib/python3.11/site-packages/pymatgen/io/ase.py", line 44, in <module>
    class MSONAtoms(Atoms, MSONable):
                    ^^^^^
NameError: name 'Atoms' is not defined
Expected Behavior
The module should still be importable in cases where ase is not available, as it was before.
Minimal example
python -m venv .venv
. .venv/bin/activate
pip install pymatgen
$ python -c "from pymatgen.analysis.magnetism.analyzer import *"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-pymatgen/lib/python3.11/site-packages/pymatgen/analysis/magnetism/__init__.py", line 5, in <module>
    from pymatgen.analysis.magnetism.analyzer import (
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-pymatgen/lib/python3.11/site-packages/pymatgen/analysis/magnetism/analyzer.py", line 24, in <module>
    from pymatgen.transformations.advanced_transformations import MagOrderingTransformation, MagOrderParameterConstraint
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-pymatgen/lib/python3.11/site-packages/pymatgen/transformations/advanced_transformations.py", line 33, in <module>
    from pymatgen.io.ase import AseAtomsAdaptor
  File "/home/mevans/repos/re2fractive/re2fractive/experiments/2_featurizing_refractive_index_data/.venv-pymatgen/lib/python3.11/site-packages/pymatgen/io/ase.py", line 44, in <module>
    class MSONAtoms(Atoms, MSONable):
                    ^^^^^
NameError: name 'Atoms' is not defined
which runs correctly if you pip install pymatgen==2024.2.8.
Relevant files to reproduce this bug
No response

Duplicate issue #3643: Importing Trajectory from pymatgen.core.trajectory gives an NameError on latest release (2024.2.20)
Created: 2024-02-22T13:57:44Z
Python version
3.11
Pymatgen version
2024.2.20
Operating system version
Any Linux
Current behavior
Importing gives a NameError:
    from pymatgen.core.trajectory import Trajectory as PymatgenTrajectory
/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/pymatgen/core/trajectory.py:19: in <module>
    from pymatgen.io.ase import AseAtomsAdaptor
/opt/hostedtoolcache/Python/3.10.13/x64/lib/python3.10/site-packages/pymatgen/io/ase.py:44: in <module>
    class MSONAtoms(Atoms, MSONable):
E   NameError: name 'Atoms' is not defined

Expected Behavior
I expect the import to be successful. It works on all versions before 2024.2.20
Minimal example
Python 3.11.7 (main, Dec  4 2023, 18:10:11) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymatgen.core.trajectory import Trajectory 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vikko/local_projects/GEMDAT/.venv/lib/python3.11/site-packages/pymatgen/core/trajectory.py", line 19, in <module>
    from pymatgen.io.ase import AseAtomsAdaptor
  File "/home/vikko/local_projects/GEMDAT/.venv/lib/python3.11/site-packages/pymatgen/io/ase.py", line 44, in <module>
    class MSONAtoms(Atoms, MSONable):
                    ^^^^^
NameError: name 'Atoms' is not defined


### Relevant files to reproduce this bug

_No response_

Closing comment (2024-02-23T07:01:34Z): Thank you for the workaround suggestion!
Closing this as duplicate of #3644