Skip to content
Happy Programming Guide
Start learning
Python

ModuleNotFoundError: No Module Named ‘_lzma’

This means Python was built without the LZMA compression library. Here is how to fix it on Linux, macOS and with pyenv, and why pip cannot help.

Hands typing on a laptop keyboard

ModuleNotFoundError: No module named '_lzma' is not a missing pip package. The leading underscore means it is a C extension that ships with Python — and yours was compiled on a machine that did not have the LZMA development library installed. The fix is to install that library and rebuild Python, not to install anything from PyPI.

Where it comes from#

Output
ModuleNotFoundError: No module named '_lzma'

Often it appears indirectly, from pandas reading a compressed file, or from tarfile handling an .xz archive:

Python
import pandas as pd
df = pd.read_csv("data.csv.xz")     # triggers it

Sometimes it arrives as a warning at import time and only becomes an error later, which makes it easy to miss the real cause.

Why pip cannot help#

Modules whose names start with an underscore are built into the interpreter itself. _lzma is the C wrapper around the system’s liblzma; the Python-level lzma module imports it. When Python is compiled, its build script checks whether the LZMA headers are present. If they are not, it simply skips that extension and carries on — no error, no warning, just a Python missing one module.

So there is nothing to install from PyPI. There is a package called lzma on PyPI, but it is an old Python 2 backport and installing it will not fix this.

Step 1: confirm the diagnosis#

Terminal
python -c "import _lzma; print('present')"
python -c "import sysconfig; print(sysconfig.get_config_var('CONFIG_ARGS'))"

If the first fails, check whether other optional modules are missing too — they usually travel together:

Python
for name in ("_lzma", "_bz2", "_sqlite3", "_ssl", "_ctypes", "readline"):
    try:
        __import__(name)
        print(name, "ok")
    except ImportError:
        print(name, "MISSING")

A list of several missing modules tells you Python was built in a minimal environment, and rebuilding will fix all of them at once.

Step 2: install the development library#

Ubuntu and Debian:

Terminal
sudo apt update
sudo apt install -y liblzma-dev

If you are going to rebuild anyway, install the whole set so you do not have to do this twice:

Terminal
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev \
  libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev \
  libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Fedora, RHEL and CentOS:

Terminal
sudo dnf install -y xz-devel
sudo dnf groupinstall -y "Development Tools"

Alpine:

Terminal
apk add xz-dev build-base

macOS with Homebrew:

Terminal
brew install xz

Step 3: rebuild Python#

Installing the library does nothing on its own — the existing Python was already compiled without it.

With pyenv, which is where this problem appears most often:

Terminal
pyenv uninstall 3.12.2
pyenv install 3.12.2
pyenv rehash

On macOS, pyenv sometimes needs to be told where Homebrew put the library:

Terminal
export LDFLAGS="-L$(brew --prefix xz)/lib"
export CPPFLAGS="-I$(brew --prefix xz)/include"
pyenv install 3.12.2

If you compiled Python from source, re-run configure and make:

Terminal
cd Python-3.12.2
./configure --enable-optimizations
make -j "$(nproc)"
sudo make altinstall

Use altinstall rather than install so you do not replace the system Python your operating system depends on.

Step 4: verify#

Terminal
python -c "import lzma; print(lzma.__file__)"
Python
import lzma

data = lzma.compress(b"hello world" * 100)
print(len(data), "compressed bytes")
print(lzma.decompress(data)[:11])     # b'hello world'

Avoiding it altogether#

The simplest route is to use a prebuilt Python rather than compiling one:

  • Docker: the official python:3.12 images include every optional module. The -alpine variants are smaller and more likely to be missing things.
  • System packages: apt install python3 gives you a fully built interpreter.
  • Conda: conda-forge builds are complete.
  • python.org installers on macOS and Windows are complete.

This error is essentially a pyenv-and-minimal-container problem. If you are not compiling Python yourself, you will rarely meet it.

Missing module Install first Breaks
_lzma liblzma-dev / xz-devel .xz files, some pandas reads
_bz2 libbz2-dev .bz2 files
_ssl libssl-dev pip itself, all HTTPS
_sqlite3 libsqlite3-dev sqlite3, Django dev server
_ctypes libffi-dev a great many packages
readline libreadline-dev arrow keys in the REPL

All have the same shape: install the development package, rebuild Python, verify.

Questions people ask#

Can I work around it without rebuilding?

Only by avoiding LZMA entirely — decompress .xz files with the xz command-line tool first, or ask for a different format. That is a workaround, not a fix, and the next library that needs it will fail again.

Why did it work before and not now?

Usually you switched interpreters — a new pyenv version, a different container image, a fresh virtual environment built on a different base Python.

Does a virtual environment inherit this?

Yes. A venv reuses the base interpreter’s standard library, so if the base Python lacks _lzma, so does every environment created from it. Fix the base.

Is it safe to reinstall Python with pyenv?

Yes. Pyenv keeps versions separate from the system Python. Your virtual environments will need recreating afterwards, so keep a requirements.txt ready.

Where to go next#

The other ModuleNotFoundError, and how to tell them apartRead next

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *