dlwpt-code icon indicating copy to clipboard operation
dlwpt-code copied to clipboard

Cannot import 'BytesType' from diskcache.core

Open Jaideepm08 opened this issue 3 years ago • 16 comments

I was trying to run "p2ch10_explore_data.ipynb", facing import error from diskcache.core

ImportError: cannot import name 'BytesType' Looks like diskcache has been updated and does not have 'BytesType' anymore. Any alternatives to "utils/disk.py"?

Jaideepm08 avatar Oct 17 '20 22:10 Jaideepm08

It seems that using the following line could fix the current issues: from cassandra.cqltypes import BytesType

And the BytesIO line should be changed to the following: from diskcache import FanoutCache, Disk,core from diskcache.core import io from io import BytesIO
from diskcache.core import MODE_BINARY

al00014 avatar Oct 18 '20 07:10 al00014

Heya, thank you for reporting this! We'll need to update the code.

t-vi avatar Oct 18 '20 09:10 t-vi

I had the same error and, as @al00014 suggested, adding the following should work: from cassandra.cqltypes import BytesType from diskcache import FanoutCache, Disk,core from diskcache.core import io from io import BytesIO from diskcache.core import MODE_BINARY

Besides that, I also needed to add cassandra-driver to the requirements.

@diogodanielsoaresferreira, I am having the same issue. I installed cassandra ('pip install cassandra-driver') and then added the following to the header of the 'disk.py' file:

from cassandra.cqltypes import BytesType from diskcache import FanoutCache, Disk,core from diskcache.core import io from io import BytesIO from diskcache.core import MODE_BINARY

Now I get the error 'cannot import name 'BytesType' from 'diskcache.core'.

Thanks in advance!

fmarti04 avatar Oct 25 '20 22:10 fmarti04

@fmarti04 you also have to delete/comment the following lines:

from diskcache import FanoutCache, Disk from diskcache.core import BytesType, MODE_BINARY, BytesIO

Does it work?

@diogodanielsoaresferreira thank you so much for replying to my message so quickly. Yes, your proposed change works. I was editing the wrong "disk.py" file. The correct "disk.py" file to edit is the one found under "/util/disk.py", not the one found under "p2ch10/util/disk.py".

Thanks again Diogo!

fmarti04 avatar Oct 26 '20 15:10 fmarti04

Thank you (all above). This helped a lot. I couldn't figure out what I was doing wrong it just kept complaining about things not being found after install and install and...

I hope the code gets updated. I just downloaded mine on Nov 17th and it is not corrected at least in Manning Pub. Maybe GitHub is more up to date. Jeff

jeffj9930 avatar Nov 18 '20 19:11 jeffj9930

Updated: I have checked the following code, and it works! image

JonathanSum avatar Jan 23 '21 11:01 JonathanSum

I used the following pip installs: !pip install cassandra-driver !pip install diskcache !pip install SimpleITK

I have to use this code to for colab "cd dlwpt-code" because I want to use the dlwpt-code as main directory.

JonathanSum avatar Jan 23 '21 11:01 JonathanSum

This pull request fixes this issue with @diogodanielsoaresferreira's solution. https://github.com/deep-learning-with-pytorch/dlwpt-code/pull/51

I am currently testing it with the p2ch10_explore_data.ipynb. https://github.com/JonathanSum/pytorch-Deep-Learning_colab/blob/master/p2ch10_explore_data.ipynb

JonathanSum avatar Jan 23 '21 12:01 JonathanSum

I face the same problem, too. "cannot import name 'BytesType' " I have tried all the solutions that mentioned above, but still not work out.

Can anyone solve the problem? Thanks again.

Updated: I have checked the following code, and it works! image

Erichoho avatar Jul 11 '22 08:07 Erichoho

@Erichoho did you use my notebook? https://github.com/JonathanSum/pytorch-Deep-Learning_colab/blob/master/p2ch10_explore_data.ipynb

JonathanSum avatar Jul 11 '22 10:07 JonathanSum

@Erichoho did you use my notebook? https://github.com/JonathanSum/pytorch-Deep-Learning_colab/blob/master/p2ch10_explore_data.ipynb

yes, and the problem still exist, whether I run in Colab or my computer. Could you please try again? Thanks.

Erichoho avatar Jul 11 '22 12:07 Erichoho

did you install "cassandra-driver" and "diskcache" package? i met the same problem and solved after those packages installed.

hwr9912 avatar Nov 24 '22 04:11 hwr9912

Reason

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.

yaner-here avatar Nov 03 '23 01:11 yaner-here

Wow,it's really useful.Thanks!

pei99527 avatar Jan 19 '24 09:01 pei99527