Last updated: Apr 10, 2024
Reading time·3 min

pyenvThe 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.
UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
# for Ubuntu sudo apt-get install -y lzma liblzma-dev # for CentOS yum install -y xz-devel

Download the Python source code form https://www.python.org/downloads/.
Go to the source code directory (e.g. Python-3.X.Y) and run the following
commands.
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.
./configure sudo make sudo make install
You can also use the make altinstall command to not overwrite the default
Python installation.
./configure sudo make sudo make altinstall
You can also use the --enable-optimizations flag to enable profile-guided optimizations.
./configure --enable-optimizations sudo make sudo make altinstall
pyenvIf you use the pyenv package to manage multiple Python versions on macOS:
xz and readline packages using homebrew.The xz package is necessary to pick up the correct lzma on macOS.
brew install readline xz
pyenv.pyenv install 3.10.6
Make sure to replace 3.10.6 with your preferred Python version.
~/.bashrc, ~/.bash_profile or ~/.zshrc)
for editing.For example, if you use bash, you can open your profile file as follows.
# With Gedit sudo gedit ~/.bashrc # Or with Nano sudo nano ~/.bashrc
Add the following line at the end of the file.
eval "$(pyenv init -)"
Then, source the updated ~/.bashrc file.
# BASH source ~/.bashrc source ~/.bash_profile
If you use ~/.zshrc, issue the following command.
sudo gedit ~/.zshrc sudo nano ~/.zshrc
Add the following line at the end of the file.
eval "$(pyenv init -)"
Source your ~/.zshrc file and restart your server.
source ~/.zshrc
Enable the installed Python version using the pyenv global command.
pyenv global 3.10.6
Make sure to specify the correct Python version - the version you previously
installed using pyenv install.
pyenv install 3.10.6
Try to install a package to verify everything works as expected.
pip install requests
If the issue persists and you are on macOS, run the following commands.
xz is installed.brew install xz
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"
# Make sure GNUBINS is not in your PATH before running the command PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.10.6
pip install requests
You can learn more about the related topics by checking out the following tutorials: