deep-learning-with-pytorch / dlwpt-code

Code for the book Deep Learning with PyTorch by Eli Stevens, Luca Antiga, and Thomas Viehmann.

Home Page:https://www.manning.com/books/deep-learning-with-pytorch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot import 'BytesType' from diskcache.core

Jaideepm08 opened this issue · 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"?

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

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

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

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

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

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.

This pull request fixes this issue with @diogodanielsoaresferreira's solution.
#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

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

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

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.

Wow,it's really useful.Thanks!