Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
pymatgen (Python Materials Genomics) is a free, MIT-licensed Python library for building, reading, writing and analysing crystal structures and molecules, and it is the core analysis code behind the Materials Project. Researchers use it to convert between structure file formats such as CIF and VASP POSCAR, to prepare computational inputs, and to run analyses such as phase diagrams, Pourbaix diagrams, defects and band structures. This guide covers what it does, how to install it, a worked file-conversion and supercell example taken from the official usage documentation, how to read what you get back, and the problems people most often hit. Facts below were checked against the PyPI record and the official pymatgen.org documentation on 2026-11-14.
What pymatgen is used for in research
The PyPI description calls pymatgen the “Python Materials Genomics” package and the core analysis code powering the Materials Project. It is object-oriented: a crystal is a Structure built from a Lattice and a list of sites, each site carrying an element or species; a finite molecule is a Molecule; and a formula is a Composition. These classes live in pymatgen.core, alongside the periodic-table classes Element and Species and the Site and PeriodicSite classes.
On top of those objects the project README lists several families of functionality:
- File input and output for VASP, ABINIT, CIF, Gaussian and XYZ formats, so a structure produced by one code can be handed to another.
- Analysis in
pymatgen.analysis: phase diagrams, Pourbaix diagrams, reaction analysis, local environments, surfaces, interfaces, defects, magnetism, piezoelectricity and elasticity. - Electronic structure objects such as density of states and band structure.
- Entries (
pymatgen.entries): the usage documentation describesComputedEntryandComputedStructureEntryas the basic unit of analysis, for example as the input to aPhaseDiagram. - External access in
pymatgen.ext, which includes the Materials Project REST API client, pluspymatgen.appsand a command-line tool,pmg.
Two design points matter for reproducible work. First, the documentation states that pymatgen uses angstroms for lengths and eV for energies by default, so any value you pass in or read out is in those units unless you convert it. Second, the VASP input sets in pymatgen.io.vasp.sets are described as consistent with the data in the Materials Project, and the documentation describes a GGA/GGA+U mixing compatibility scheme for combining computed entries. If you intend to compare your own calculations with Materials Project data, those two pieces are where the match or mismatch is decided; an input set that differs from the one used for the reference data is a methodological difference to report, not a software detail.
pymatgen is not a general statistics package, and it does not run electronic-structure calculations itself. It prepares inputs for, and parses outputs from, other programs, and analyses the results. The idea of treating research code as a citable output appears in the dictionary entry for JOSS (Journal of Open Source Software).
Install or access pymatgen from the official source
The package is published on PyPI as pymatgen; the PyPI record read on 2026-11-14 lists version 2026.10.24, the MIT license and a requirement of Python 3.11 or newer. The documentation site is pymatgen.org and the source repository is github.com/materialsproject/pymatgen.
pymatgen or pymatgen-core?
The README explains that the core objects (Element, Site, Molecule, Structure, Composition, Lattice) and file input and output now live in a lean package called pymatgen-core. Installing pymatgen pulls in pymatgen-core plus the higher-level analysis code, so install pymatgen if you want phase diagrams, defects or the other analyses listed above, and consider the lean core package only if you need just structures and file conversion.
Install with pip
The installation page states that dependencies are installed automatically when you use pip:
pip install pymatgen
Use a virtual environment so the version you cite is the version you ran. Because the current release needs Python 3.11 or newer, check python --version first.
Install with conda
The documented conda route uses the conda-forge channel:
conda install --channel conda-forge pymatgen
The installation page also gives a fallback for conda environments where that fails: install the numerical stack first, then pymatgen with pip.
conda install --yes numpy scipy matplotlib
pip install pymatgen
Optional dependencies and external programs
Some features need extra software that pip does not install by default. The data table below maps tasks to modules and to the optional dependency the installation page names for each. For visualisation the documented extra is pip install "pymatgen[vis]", which brings in VTK 9.6 or newer. For the enumeration program enumlib and for bader, the page describes pmg config --install enumlib and pmg config --install bader on OSX and Linux. The documentation recommends Enumlib.jl first; the pmg command compiles the legacy Fortran enumlib.
Step-by-step: read a POSCAR, write a CIF and build a supercell
This worked example uses only calls shown in the official usage documentation. It assumes you have a VASP structure file named POSCAR in the working directory.
- Import the core classes. In Python, run
from pymatgen.core import Lattice, Structure, Molecule. - Read the structure.
structure = Structure.from_file("POSCAR")reads the file and returns aStructureobject. The documentation notes that the format is guessed from the filename. - Write it as a CIF.
structure.to(filename="CsCl.cif")writes the structure in the format implied by the filename extension. The documentation also shows the explicit route:Poscar.from_file("POSCAR").structureto read, andCifWriter(poscar.structure).write_file("mystructure.cif")to write, using the classes inpymatgen.io. - Build a supercell.
structure.make_supercell([2, 2, 2])scales the cell by two along each lattice direction. Note that this changes the object in place, becauseStructureandMoleculeare mutable; the immutable variants areIStructureandIMolecule. - Find the primitive cell.
structure.get_primitive_structure()returns the primitive version of a structure. - Interpolate between two structures.
structure.interpolate(another_structure, nimages=10)generates intermediate images, useful for pathways between two endpoints.
To create a structure from scratch instead of from a file, the documentation builds a lattice with Lattice.from_parameters(a=3.84, b=3.84, c=3.84, alpha=120, beta=90, gamma=60) and passes it, with a list of species and coordinates, to the Structure constructor, for example Structure(lattice, ["Si", "Si"], coords). Lengths are in angstroms and angles are in degrees.
Output and how to read it
Most pymatgen output is an object you inspect in Python rather than a report. A Structure printed in a notebook shows its lattice parameters and each site with its species and coordinates; a written CIF or POSCAR is a plain text file you can open in any structure viewer or hand to another code. Three habits make that output trustworthy.
- Check units. The documented defaults are angstroms and eV. If another source gives lengths in bohr or energies in hartree, convert before comparing.
- Save objects losslessly. The usage documentation describes
as_dictandfrom_dict(provided through the monty library) for saving objects to JSON or YAML, withMontyEncoderandMontyDecoderfor serialising nested objects. Archiving a structure as a dictionary is more reproducible than re-typing coordinates. - Record versions. Write down the pymatgen version (
pip show pymatgen) and Python version with your results. The package uses calendar versions, such as 2026.10.24, so the version string is itself a date.
For analyses such as phase diagrams the documentation says the basic unit is a computed entry, so what you read out depends on the energies you supplied. That makes the energy-correction scheme you used part of your methods, in the same way as the parameters in a FAIR-aligned data record.
Troubleshooting
pip cannot find a compatible version
The current release requires Python 3.11 or newer. On an older interpreter pip will not select the latest version; create an environment with a supported Python rather than pinning an old release unless you must reproduce older work.
An import fails for a specific feature
Features such as defect analysis, visualisation, ASE interoperability, Open Babel conversion, networkx-based tools and numba acceleration depend on optional packages that are not installed by default. The data table below lists the ones the installation page names. Install the one named for your task.
Visualisation does not work
Install the visualisation extra with pip install "pymatgen[vis]"; the installation page lists VTK 9.6 or newer as its requirement.
Enumeration or bader tools are missing
These are separate programs, not Python packages. The documentation names enum.x and makestr.x from enumlib, notes that Enumlib.jl is recommended, and describes pmg config --install enumlib (which compiles the legacy Fortran enumlib) and pmg config --install bader for OSX and Linux.
My supercell changed the original structure
Mutability is by design. If you need the original later, copy it first or use IStructure, the immutable class.
Where to get help
The README points to the MatSci forum at matsci.org/pymatgen and to GitHub issues and discussions; example notebooks are collected in matgenb.
Related tools
The pymatgen documentation itself lists interoperability with two other packages, which is the clearest sourced guide to the neighbouring tools.
| Tool | How pymatgen documents it |
|---|---|
| ASE (Atomic Simulation Environment) | Optional dependency; the module pymatgen.io.aseio converts between the two libraries’ structure objects. |
| Open Babel | Optional dependency; the module pymatgen.io.babelio gives access to more than 100 file types. |
| Materials Project API | Reached from pymatgen through pymatgen.ext; the project describes pymatgen as the core analysis code powering the Materials Project. |
None of these replaces pymatgen so much as extends it. Choose by the file formats and analyses your calculation chain needs, and state each tool and version in your methods section.
How to cite pymatgen
The PyPI page links the citation: Ong et al., Computational Materials Science, 2012, doi:10.1016/j.commatsci.2012.10.028. Cite that paper and give the version you used. The cited paper appeared in the journal covered in our guide to Computational Materials Science.
Frequently asked questions
What is pymatgen used for?
It is a Python library for working with crystal structures and molecules: reading and writing formats such as CIF, POSCAR, ABINIT, Gaussian and XYZ, and analysing them, including phase diagrams, Pourbaix diagrams, defects, surfaces, band structure and elasticity. It is the core analysis code of the Materials Project.
Is pymatgen free?
The PyPI record states the MIT license and the README states that pymatgen is free to use.
Which Python version does pymatgen need?
The release read on 2026-11-14, 2026.10.24, requires Python 3.11 or newer.
What is the difference between pymatgen and pymatgen-core?
pymatgen-core is the lean package holding the core objects and file input and output; installing pymatgen also installs higher-level analysis code.
What units does pymatgen use?
Angstroms for length and eV for energy by default, according to the usage documentation.
How do I convert a POSCAR to a CIF with pymatgen?
Read it with Structure.from_file("POSCAR") and write it with structure.to(filename="name.cif"); the format is guessed from the filename.
Where can I ask questions?
The MatSci forum and the project’s GitHub issues and discussions.
Sources
- PyPI, pymatgen project record and README, pypi.org/project/pymatgen, read 2026-11-14.
- pymatgen documentation, Installation, pymatgen.org/installation, read 2026-11-14.
- pymatgen documentation, Usage, pymatgen.org/usage, read 2026-11-14.
- pymatgen source repository, github.com/materialsproject/pymatgen.
Data: pymatgen tasks, modules and optional dependencies named in the official documentation
Compiled from the pymatgen installation and usage pages and the PyPI README: for each task, the module that handles it and the optional package or external program the installation page names for it.
| Task | pymatgen module or command | Optional dependency named in the docs |
|---|---|---|
| Read and write CIF, POSCAR and other structure files | pymatgen.core, pymatgen.io | None beyond pip install pymatgen |
| Defect analysis | pymatgen.analysis | sympy |
| 3D visualisation | pip install "pymatgen[vis]" | VTK 9.6 or newer |
| Convert to and from ASE objects | pymatgen.io.aseio | ASE |
| Read and write more than 100 file types | pymatgen.io.babelio | OpenBabel |
| Enumerate ordered structures | enumlib (enum.x, makestr.x); pmg config --install enumlib | enumlib (Enumlib.jl recommended) |
Sources
- pymatgen installation (accessed 2026-11-14)
- pymatgen usage (accessed 2026-11-14)
- pymatgen on PyPI (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







