Pymatgen: Python Materials Genomics

2026.3.23 · active · verified Sun Apr 12

Pymatgen (Python Materials Genomics) is a robust materials analysis code that defines core object representations for crystal structures, molecules, and electronic structure data. It powers the Materials Project and offers extensive tools for materials design, data analysis, and high-throughput computations. The current version is 2026.3.23, and it maintains a rapid release cadence, often with monthly or bi-monthly updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic `Structure` object and interact with the Materials Project database using `MPRester`. An `MP_API_KEY` environment variable is required for `MPRester` functionality.

import os
from pymatgen.core import Structure, Lattice, Species
from pymatgen.ext.matproj import MPRester

# 1. Create a simple crystal structure (e.g., BCC iron)
lattice = Lattice.cubic(2.86)
species = [Species("Fe")]
coords = [[0, 0, 0]]
structure = Structure(lattice, species, coords)
print(f"Created structure: {structure.formula} with {structure.num_sites} sites.")

# 2. Use MPRester to fetch data (requires API key)
# Get your API key from materialsproject.org after logging in
# Set it as an environment variable 'MP_API_KEY'
api_key = os.environ.get("MP_API_KEY", "")

if api_key:
    try:
        with MPRester(api_key) as mpr:
            # Fetch entries for a chemical system, e.g., Li-Fe-O
            entries = mpr.get_entries("Li-Fe-O", inc_structure=True, property_data=["band_gap"])
            print(f"Found {len(entries)} entries for Li-Fe-O.")
            if entries:
                first_entry = entries[0]
                print(f"First entry formula: {first_entry.formula_pretty}")
                if "band_gap" in first_entry.data:
                    print(f"Band gap: {first_entry.data['band_gap']} eV")
    except Exception as e:
        print(f"Error fetching data from Materials Project: {e}")
        print("Ensure your MP_API_KEY is valid and has network access.")
else:
    print("MP_API_KEY environment variable not found. Skipping MPRester example.")

view raw JSON →