deep-learning-with-pytorch / dlwpt-code

Code for the book Deep Learning with PyTorch by Eli Stevens, Luca Antiga, and Thomas Viehmann.
https://www.manning.com/books/deep-learning-with-pytorch
4.69k stars 1.98k forks source link

Solution for import error: "cannot import name 'BytesType' from 'diskcache.core'" #116

Closed AliSerwat closed 2 months ago

AliSerwat commented 2 months ago

Resolved import error 'cannot import name 'BytesType' from 'diskcache.core'' in '/dlwpt-code/p2ch10_explore_data.ipynb' by updating the import statement in 'util/disk.py' .

This is a legacy issue. By inspecting the commit history of the python-diskcache library, you will find that diskcache.core.BytesType was essentially str (in Python 2) or bytes (in Python 3), which was added and then removed.

commit 4497cfc85197d57298120dbd238d263bfb9e9557
Author: Grant Jenks <grant.jenks@gmail.com>
Date:   Sat Aug 22 22:58:07 2020 -0700

-if sys.hexversion < 0x03000000:
-    ......
-    BytesType = str
-    ......
-else:
-    ......
-    BytesType = bytes
-    ......

And diskcache.core.BytesIO is StringIO.cStringIO/StringIO.StringIO (in Python 2) and io.BytesIO (in Python 3).

+if sys.hexversion < 0x03000000:
+    range = xrange
+    try:
+        from cStringIO import StringIO as BytesIO
+    except ImportError:
+        from StringIO import StringIO as BytesIO
+
+else:
+    import io
+    BytesIO = io.BytesIO

Solution

Since we have known what the BytesType and BytesIO should be, let's edit the /util/disk.py directly:

from diskcache import FanoutCache, Disk
from diskcache.core import MODE_BINARY # delete BytesType and BytesIO declarations

BytesType = bytes # Import them by ourselves
import io
BytesIO = io.BytesIO

And that works for me.

Originally posted by @yaner-here in https://github.com/deep-learning-with-pytorch/dlwpt-code/issues/27#issuecomment-1791794582