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.76k stars 2.01k forks source link

Cannot import 'BytesType' from diskcache.core #27

Open Jaideepm08 opened 4 years ago

Jaideepm08 commented 4 years ago

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"?

al00014 commented 4 years ago

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

t-vi commented 4 years ago

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

diogodanielsoaresferreira commented 4 years ago

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.

fmarti04 commented 4 years ago

@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!

diogodanielsoaresferreira commented 4 years ago

@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?

fmarti04 commented 4 years ago

@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!

jeffj9930 commented 4 years ago

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

JonathanSum commented 3 years ago

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

JonathanSum commented 3 years ago

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 commented 3 years ago

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

Erichoho commented 2 years ago

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

JonathanSum commented 2 years ago

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

Erichoho commented 2 years ago

@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.

hwr9912 commented 2 years ago

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

yaner-here commented 1 year ago

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 <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.

pei99527 commented 10 months ago

Wow,it's really useful.Thanks!