vision: ModuleNotFoundError: No module named '_lzma'

šŸ› Bug

Importing the latest version of torchvision==0.8.1 throws following error:

  File "tor-example.py", line 2, in <module>
    import torchvision
  File "/home/zoran/MyProjects/mindsdb-examples/tor/lib/python3.7/site-packages/torchvision/__init__.py", line 7, in <module>
    from torchvision import datasets
  File "/home/zoran/MyProjects/mindsdb-examples/tor/lib/python3.7/site-packages/torchvision/datasets/__init__.py", line 6, in <module>
    from .mnist import MNIST, EMNIST, FashionMNIST, KMNIST, QMNIST
  File "/home/zoran/MyProjects/mindsdb-examples/tor/lib/python3.7/site-packages/torchvision/datasets/mnist.py", line 11, in <module>
    import lzma
  File "/usr/local/lib/python3.7/lzma.py", line 27, in <module>
    from _lzma import *
ModuleNotFoundError: No module named '_lzma'

To Reproduce

Steps to reproduce the behavior:

  1. pip install torchvision
  2. import torchvision

Environment

CUDA used to build PyTorch: 10.2
ROCM used to build PyTorch: N/A

OS: Ubuntu 19.10 (x86_64)
GCC version: (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
Clang version: Could not collect
CMake version: version 3.13.4

Python version: 3.7 (64-bit runtime)
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] torch==1.7.0
[pip3] torchvision==0.8.1
[conda] Could not collect
  • PyTorch / torchvision Version (1.7.0 / 0.8.1):
  • OS : Linux Ubuntu
  • How you installed PyTorch / torchvision (conda, pip, source): pip
  • Python version: 3.7.3

cc @pmeier

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18

Most upvoted comments

Hi, I came across this issue after installing python from source. It seems that I installed python without liblzma-dev. After installing liblzma-dev and installing python again, the import error was gone.

Thus issue occurs in 3.10 but not in 3.7 for me.

In Ubuntu 20.04, I had to install lzma lzma-dev and liblzma-dev

and compile again Python 3.7 using:

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

for centos 7,

  1. yum install -y xz-devel
  2. compile python3 again

If you do NOT want to re-compile python, you can use this method to fix the problem: https://support.huawei.com/enterprise/en/doc/EDOC1100289998/db0db8f0/modulenotfounderror-no-module-named-_lzma-

Cannot confirm. In f9c4fdf956ba55dd893f8f4a37d986845cdd52b8 we changed this from a lazy import. Given that lzma is part of the standard library since at least Python 3.5 this should not be a problem.

@ZoranPandovski lzma is part of python standard libs: https://docs.python.org/3.7/library/lzma.html I’d say there can be a problem with your python. See here if it can help you : https://github.com/pandas-dev/pandas/issues/27532#issuecomment-514044754