Skip to content
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

Cannot import 'BytesType' from diskcache.core #27

Open
Jaideepm08 opened this issue Oct 17, 2020 · 16 comments
Open

Cannot import 'BytesType' from diskcache.core #27

Jaideepm08 opened this issue Oct 17, 2020 · 16 comments

Comments

@Jaideepm08
Copy link

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
Copy link

al00014 commented Oct 18, 2020

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 t-vi reopened this Oct 18, 2020
@t-vi
Copy link

t-vi commented Oct 18, 2020

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

@diogodanielsoaresferreira

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
Copy link

@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

@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
Copy link

@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
Copy link

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
Copy link

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

JonathanSum added a commit to JonathanSum/dlwpt-code that referenced this issue Jan 23, 2021
deep-learning-with-pytorch#27 27
after using the new import, it works
@JonathanSum
Copy link

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
Copy link

JonathanSum commented Jan 23, 2021

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

LucioAmely added a commit to LucioAmely/dlwpt-code that referenced this issue Apr 18, 2021
@Erichoho
Copy link

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
Copy link

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

@Erichoho
Copy link

@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
Copy link

hwr9912 commented Nov 24, 2022

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

@yaner-here
Copy link

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.

@pei99527
Copy link

Wow,it's really useful.Thanks!

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