AttributeError: 'str' object has no attribute 'decode'

Can someone please help with this?

urls = search_images_ddg('grizzly bear', max_images=100)
len(urls),urls[0]s

---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-4-fe39ec9b75f1> in <module>
----> 1 urls = search_images_ddg('grizzly bear', max_images=100)
      2 len(urls),urls[0]

~\anaconda3\lib\site-packages\fastbook\__init__.py in search_images_ddg(term, max_images)
     55     assert max_images<1000
     56     url = 'https://duckduckgo.com/'
---> 57     res = urlread(url,data={'q':term}).decode()
     58     searchObj = re.search(r'vqd=([\d-]+)\&', res)
     59     assert searchObj

AttributeError: 'str' object has no attribute 'decode'
1 Like

Can you please prit what urlread returns?
print(urlread(url,data={‘q’:term})
Maybe it returned an error message (a string) that cannot be decoded?

1 Like

urlread now returns urls decoded as str per default, you can remove the .decode() from the function and it works.

2 Likes

Removed the .decode() recommended by @BresNet, fixed the issue. :slight_smile:

seems that the search_images_ddg() in (even the latest) fastbook still has the “.decode” in it.

urls = search_images_ddg('grizzly bear', max_images=100)
len(ims)

urls = search_images_ddg('grizzly bear', max_images=100)
len(ims)
urls = search_images_ddg('grizzly bear', max_images=100)
len(ims)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-e3064b7d2a3d> in <module>
----> 1 urls = search_images_ddg('grizzly bear', max_images=100)
      2 len(ims)

~/envs/fastai/lib/python3.8/site-packages/fastbook/__init__.py in search_images_ddg(term, max_images)
     55     assert max_images<1000
     56     url = 'https://duckduckgo.com/'
---> 57     res = urlread(url,data={'q':term}).decode()
     58     searchObj = re.search(r'vqd=([\d-]+)\&', res)
     59     assert searchObj

AttributeError: 'str' object has no attribute 'decode'

How do you remove the .decode function, since it is in /usr/local/lib/python3.7/dist-packages/fastbook/__init__.py which in the library path?
is it okay to remove it from inbuilt files?

It is fixed in the book’s repo. I guess it hasn’t published to pypy yet.

You can copy the whole function from fastbook/utils.py at 8be580737ee0cc17746a5ed68283150d489b3dc4 · fastai/fastbook · GitHub, add a new cell in a notebook, and run it. It will override the installed package’s function.

1 Like

Thank you @delgermurun for fixing it. how can we publish it to pypy please?