# ModuleNotFoundError: No module named '\_lzma'

**URL:** <https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894>\
**Category:** vision\
**Created:** [April 30, 2021, 8:17am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894 "2021-04-30T08:17:21Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 8:17am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/1 "2021-04-30T08:17:22Z")

</div>

I’m using python 3.7.10 with torch 1.81 and torchvision 0.9.1 in my machine and while importing dset from torchvision.dataset this error shows up:

```auto
Traceback (most recent call last):
  File "train.py", line 23, in <module>
    from dataloader import load_data
  File "/home/rdb/Prithviraj/Image-Restoration-in-Occluded-Images-using-GANs/dataloader.py", line 3, in <module>
    import torchvision.datasets as dset
  File "/home/rdb/.local/lib/python3.7/site-packages/torchvision/ __init__.py", line 7, in <module>
    from torchvision import datasets
  File "/home/rdb/.local/lib/python3.7/site-packages/torchvision/datasets/ __init__.py", line 6, in <module>
    from .mnist import MNIST, EMNIST, FashionMNIST, KMNIST, QMNIST
  File "/home/rdb/.local/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'

```

This means that the torchvision I have is broken or buggy. Can someone suggest a fix for this error.  
If u need a demo code to run then please use [this](https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html) tutorial .

Any help would be great!!

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2021, 8:31am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/2 "2021-04-30T08:31:10Z")

</div>

It seems that the usage of `lzma` was removed in the [master branch](https://github.com/pytorch/vision/blob/58afa5110ce65c718dea676eccc5783d7e56d07a/torchvision/datasets/mnist.py#L9), but for `0.9.1` you could try to install it via `pip` or `conda`.

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 8:34am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/3 "2021-04-30T08:34:16Z")

</div>

is it this command?  
`pip install backports.lzma`

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2021, 8:38am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/4 "2021-04-30T08:38:00Z")

</div>

This could work. However, note that `lzma` should be a default Python package starting from [Python 3.5](https://docs.python.org/3.5/library/lzma.html), so unsure why it’s missing in your environment.  
A related issue is [here](https://github.com/pytorch/vision/issues/2921) with more suggestions.

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 8:45am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/5 "2021-04-30T08:45:19Z")

</div>

Ya, i have read this thread and it says that the fix is to do lazy import or something. But I don’t really understand it as \_lmza should be imported by default when torchvision is imported. and as you said it is a default python package but the difference is I’m using python version 3.7.10 and you have mentioned about python3.5  
Any suggestions what I should do next?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2021, 8:51am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/6 "2021-04-30T08:51:52Z")

</div>

You could follow [this advice](https://github.com/pytorch/vision/issues/2921#issuecomment-811767491), install the missing library, and then reinstall Python so that it’ll add the support. Alternatively, you could also search for other standalone Python libs (such as in your previous suggestion), but I haven’t tried to install any of them.

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 8:56am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/7 "2021-04-30T08:56:16Z")

</div>

I tried to install that package but it says that the package is already up to date

```auto
sudo apt-get install -y liblzma-dev

Reading package lists... Done
Building dependency tree       
Reading state information... Done
liblzma-dev is already the newest version (5.1.1alpha+20120614-2ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.

```

So it wasn’t missing in the first place…

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 8:59am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/8 "2021-04-30T08:59:11Z")

</div>

So there might a issue in torchvision.

> [@ptrblck](#):
>
> A related issue is [here](https://github.com/pytorch/vision/issues/2921) with more suggestions.

The thread here says that torchvision works fine with version 0.7.0

do u recommend downgrading the version? (if so then please also show how to)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2021, 9:49am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/9 "2021-04-30T09:49:03Z")

</div>

I don’t think it’s an issue in `torchvision`, as `lzma` should be a default Python package, and thus it would be a valid dependency.  
I don’t know why your Python setup isn’t recognizing it, so you might need to create a new environment maybe.

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 11:40am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/10 "2021-04-30T11:40:51Z")

</div>

I listed down all the packages of python using `help("modules")`

```auto
PIL _weakrefset heapq scipy
__future__ _xxtestfuzz hmac seaborn
_abc abc html secrets
_ast absl http select
_asyncio aifc idlelib selectors
_bisect antigravity imaplib setuptools
_blake2 argparse imghdr shelve
_bootlocale array imp shlex
_bz2 ast importlib shutil
_codecs asynchat inspect signal
_codecs_cn asyncio io site
_codecs_hk asyncore ipaddress six
_codecs_iso2022 atexit itertools smtpd
_codecs_jp audioop json smtplib
_codecs_kr base64 keyword sndhdr
_codecs_tw bdb kiwisolver socket
_collections binascii lib2to3 socketserver
_collections_abc binhex linecache spwd
_compat_pickle bisect locale sqlite3
_compression builtins logging sre_compile
_contextvars bz2 lzma sre_constants
_crypt cProfile macpath sre_parse
_csv caffe2 mailbox ssl
_ctypes calendar mailcap stat
_ctypes_test cgi marshal statistics
_curses cgitb math string
_curses_panel chunk matplotlib stringprep
_datetime cmath mimetypes struct
_dbm cmd mmap subprocess
_decimal code modulefinder sunau
_dummy_thread codecs multiprocessing symbol
_elementtree codeop netrc symtable
_functools collections nis sys
_gdbm colorsys nntplib sysconfig
_hashlib compileall ntpath syslog
_heapq concurrent nturl2path tabnanny
_imp configparser numbers tarfile
_io contextlib numpy telnetlib
_json contextvars opcode tempfile
_locale copy operator termios
_lsprof copyreg optparse test
_markupbase crypt os textwrap
_md5 csv ossaudiodev this
_multibytecodec ctypes pandas threading
_multiprocessing curses parser time
_opcode cycler pathlib timeit
_operator dataclasses pdb tkinter
_osx_support datetime pickle token
_pickle dateutil pickletools tokenize
_posixsubprocess dbm pip torch
_py_abc decimal pipes torchaudio
_pydecimal difflib pkg_resources torchvision
_pyio dis pkgutil trace
_queue distutils platform traceback
_random doctest plistlib tracemalloc
_remote_module_non_sriptable dummy_threading poplib tty
_sha1 easy_install posix turtle
_sha256 email posixpath turtledemo
_sha3 encodings pprint types
_sha512 ensurepip profile typing
_signal enum pstats typing_extensions
_sitebuiltins errno pty unicodedata
_socket faulthandler pwd unittest
_sqlite3 fcntl py_compile urllib
_sre filecmp pyclbr uu
_ssl fileinput pydoc uuid
_stat fnmatch pydoc_data venv
_string formatter pyexpat warnings
_strptime fractions pylab wave
_struct ftplib pyparsing weakref
_symtable functools pytz webbrowser
_sysconfigdata_m_linux_x86_64-linux-gnu gc queue wsgiref
_testbuffer genericpath quopri xdrlib
_testcapi get-pip random xml
_testimportmultiple getopt re xmlrpc
_testmultiphase getpass readline xxlimited
_thread gettext reprlib xxsubtype
_threading_local glob resource zipapp
_tracemalloc grp rlcompleter zipfile
_warnings gzip runpy zipimport
_weakref hashlib sched zlib

```

and as you can see there is no package called \_lmza  
So know that we have established that I don’t have this package, how should I add it ?  
By now, I have tried using the following commands:

```auto
pip install backports.lzma
pip install pylzma
sudo apt-get install liblzma-dev

```

For the first two there is a error with a long traceback call.  
while the 3rd command give this output:

> [@Xanthan](#):
>
> ```auto
> Reading package lists... Done
> Building dependency tree       
> Reading state information... Done
> liblzma-dev is already the newest version (5.1.1alpha+20120614-2ubuntu2).
> 0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
> 
> ```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2021, 7:01pm UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/11 "2021-04-30T19:01:47Z")

</div>

The `_lzma` module is internal and your list shows indeed an available `lzma` module.  
In my current environment it’s located at:

```auto
/anaconda3/envs/pytorch_181_cuda102/lib/python3.8/lzma.py

```

Could you check, if a direct import also fails:

```python
import lzma

```

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 7:15pm UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/12 "2021-04-30T19:15:27Z")

</div>

yes the import fails.

```auto
import lmza
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'lmza'

```

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [April 30, 2021, 7:30pm UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/13 "2021-04-30T19:30:25Z")

</div>

Something still seems to be strange in your Python setup.  
Which Python version are you using and are you seeing the `lzma.py` file in the `pythonMAJOR.MINOR` dir?

---

<div class="post-metadata">

**Author:** ![Xanthan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xanthan/32/37484_2.png) [@Xanthan](https://discuss.pytorch.org/u/Xanthan)\
**Post date:** [April 30, 2021, 7:59pm UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/14 "2021-04-30T19:59:35Z")

</div>

I’m using python 3.7.10.

> [@ptrblck](#):
>
> `lzma.py` file in the `pythonMAJOR.MINOR` dir?

Can you please mention where can I find the pythonMAJOR.MINOR?

---

<div class="post-metadata">

**Author:** ![Qazaq159](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/qazaq159/32/56082_2.png) [@Qazaq159](https://discuss.pytorch.org/u/Qazaq159)\
**Post date:** [January 1, 2023, 9:00am UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/15 "2023-01-01T09:00:57Z")

</div>

Reinstall env libs helped me to solve this issue. Recommend to try again(resinstall python, reinstall virtualenv and its requirements libs).

---

<div class="post-metadata">

**Author:** ![bolkogor](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bolkogor/32/68454_2.png) [@bolkogor](https://discuss.pytorch.org/u/bolkogor)\
**Post date:** [March 13, 2024, 7:21pm UTC](https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-lzma/119894/16 "2024-03-13T19:21:01Z")

</div>

I had the same issue using debian, and solved it by removing lzma import in `{venv_dir}/.../torchvision/datasets/utils.py`  
On line ~240 of `utils.py` I replaced lzma with:

```auto
def raise_lzma(*args, **kwargs):
    raise Exception('lzma removed error')

_COMPRESSED_FILE_OPENERS: Dict[str, Callable[..., IO]] = {
    ".bz2": bz2.open,
    ".gz": gzip.open,
    ".xz": raise_lzma,
}

```

It works if you don’t need compression and do not have sudo access to install `liblzma-dev` and recompile python.
