UserWarning: Could not import the lzma module. Your installed Python is incomplete

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# Table of Contents

  1. UserWarning: Could not import the lzma module. Your installed Python is incomplete
  2. Resolve the issue when using pyenv

# UserWarning: Could not import the lzma module. Your installed Python is incomplete

The Python "UserWarning: Could not import the lzma module. Your installed Python is incomplete." warning is shown when you try to compile Python from source but you don't have the lzma-dev package installed.

To solve the error, install the liblzma-dev package and recompile Python.

Here is the complete warning message.

shell
UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
  1. Open your terminal and run the command that relates to your flavor of Linux.
shell
# for Ubuntu sudo apt-get install -y lzma liblzma-dev # for CentOS yum install -y xz-devel

install liblzma dev package

  1. Download the Python source code form https://www.python.org/downloads/.

  2. Go to the source code directory (e.g. Python-3.X.Y) and run the following commands.

shell
configure sudo make sudo make install

The configure file should be located in your Python source code directory (e.g. Python-3.X.Y).

You can also try to access it using a relative path.

shell
./configure sudo make sudo make install

You can also use the make altinstall command to not overwrite the default Python installation.

shell
./configure sudo make sudo make altinstall

You can also use the --enable-optimizations flag to enable profile-guided optimizations.

shell
./configure --enable-optimizations sudo make sudo make altinstall

# Resolve the issue when using pyenv

If you use the pyenv package to manage multiple Python versions on macOS:

  1. Install the xz and readline packages using homebrew.

The xz package is necessary to pick up the correct lzma on macOS.

shell
brew install readline xz
  1. Install your preferred Python version using pyenv.
shell
pyenv install 3.10.6

Make sure to replace 3.10.6 with your preferred Python version.

  1. Open your profile file (e.g. ~/.bashrc, ~/.bash_profile or ~/.zshrc) for editing.

For example, if you use bash, you can open your profile file as follows.

shell
# With Gedit sudo gedit ~/.bashrc # Or with Nano sudo nano ~/.bashrc

Add the following line at the end of the file.

~/.bashrc
eval "$(pyenv init -)"

Then, source the updated ~/.bashrc file.

shell
# BASH source ~/.bashrc source ~/.bash_profile

If you use ~/.zshrc, issue the following command.

shell
sudo gedit ~/.zshrc sudo nano ~/.zshrc

Add the following line at the end of the file.

~/.zhsrc
eval "$(pyenv init -)"

Source your ~/.zshrc file and restart your server.

shell
source ~/.zshrc

Enable the installed Python version using the pyenv global command.

shell
pyenv global 3.10.6

Make sure to specify the correct Python version - the version you previously installed using pyenv install.

shell
pyenv install 3.10.6

Try to install a package to verify everything works as expected.

shell
pip install requests

If the issue persists and you are on macOS, run the following commands.

  1. Make sure xz is installed.
shell
brew install xz
  1. Set the following environment variables.
shell
prefix=$(brew --prefix) export LDFLAGS="-L$prefix/opt/xz/lib $LDFLAGS" export CPPFLAGS="-I$prefix/opt/xz/include $CPPFLAGS" export PKG_CONFIG_PATH="$prefix/opt/xz/lib/pkgconfig:$PKG_CONFIG_PATH"
  1. Install your preferred Python version
shell
# Make sure GNUBINS is not in your PATH before running the command PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.10.6
  1. Try to install a package to verify everything works as expected.
shell
pip install requests

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.