Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AliSerwat opened this issue Jul 4, 2024 · 0 comments

Comments

@AliSerwat
Copy link

AliSerwat commented Jul 4, 2024

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 <[email protected]>
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 #27 (comment)

@AliSerwat AliSerwat changed the title 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' . Solution for import error: 'cannot import name 'BytesType' from 'diskcache.core'' Jul 4, 2024
@AliSerwat AliSerwat changed the title Solution for import error: 'cannot import name 'BytesType' from 'diskcache.core'' Solution for import error: "cannot import name 'BytesType' from 'diskcache.core'" Jul 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant