pokebase icon indicating copy to clipboard operation
pokebase copied to clipboard

FileNotFoundError: [Errno 2] No such file or directory: 'resource.json'

Open wuwayne opened this issue 6 years ago • 12 comments

code:

#win64 python3.6.5
import pokebase as pb

chesto = pb.NamedAPIResource('berry', 'chesto')

print(chesto.name)

first time error:

requests.exceptions.HTTPError: 503 Server Error: Service Temporarily Unavailable for url: http://pokeapi.co/api/v2/berry

then always:

FileNotFoundError: [Errno 2] No such file or directory: 'resource.json'

wuwayne avatar Jun 17 '18 03:06 wuwayne

I don't understand your issue, could you please clarify? It appears to me that the second error occurred as a result of the first, and the first happened because the PokeAPI server was unable to handle your request (I have no control over that). It would help me help you if you could give more specifics about the exact steps taken here.

GregHilmes avatar Jun 18 '18 00:06 GregHilmes

Sorry for not express clearly before.

I dig into a little and get this: DDoS protection by Cloudflare.It seems that the site use Cloudflare to prevent from DDoS attack so cause the 503 Server Error.

  <div class="attribution">
    <a href="https://www.cloudflare.com/5xx-error-landing?utm_source=iuam" target="_blank" style="font-size: 12px;">DDoS protection by Cloudflare</a>
    <br>
    Ray ID: 42cbaa2190e37c28
  </div>

I try to add headers to the request,but still not working.Maybe my IP address is banned.I will continue to play with it when I get the time.

If you have any good idea pls let me know thx.

wuwayne avatar Jun 18 '18 06:06 wuwayne

Can you access the API from your browser? If you are being blacklisted by Cloudflare or PokeAPI, I'd like to know that's the case, and not the wrapper. If you still cannot access API data in your browser window, head on over to the PokeAPI Slack, and let those folks know what's going on (If you have not done so already).

GregHilmes avatar Jun 18 '18 14:06 GregHilmes

Yes.site:https://pokeapi.co/api/v2/berry/ return:

{"count":64,"previous":null,"results":[{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/1\/","name":"cheri"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/2\/","name":"chesto"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/3\/","name":"pecha"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/4\/","name":"rawst"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/5\/","name":"aspear"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/6\/","name":"leppa"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/7\/","name":"oran"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/8\/","name":"persim"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/9\/","name":"lum"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/10\/","name":"sitrus"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/11\/","name":"figy"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/12\/","name":"wiki"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/13\/","name":"mago"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/14\/","name":"aguav"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/15\/","name":"iapapa"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/16\/","name":"razz"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/17\/","name":"bluk"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/18\/","name":"nanab"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/19\/","name":"wepear"},
{"url":"https:\/\/pokeapi.co\/api\/v2\/berry\/20\/","name":"pinap"}],
"next":"https:\/\/pokeapi.co\/api\/v2\/berry\/?limit=20&offset=20"}

wuwayne avatar Jun 19 '18 02:06 wuwayne

Input:

import pokebase as pb
print(pb.pokemon(1))

Output/ Error:

Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    pb.pokemon(1)
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\loaders.py", line 483, in pokemon
    return NamedAPIResource('pokemon', id_or_name)
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 319, in __init__
    self.load()
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 367, in load
    self.__setattr__(k, [make_obj(i) for i in v])
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 367, in <listcomp>
    self.__setattr__(k, [make_obj(i) for i in v])
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 278, in make_obj
    return APIMetadata(d)
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 464, in __init__
    self.__setattr__(k, make_obj(v))
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 276, in make_obj
    return NamedAPIResource(location, name, False)
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 311, in __init__
    n = APIResourceList(r).id_to_name(name)
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 395, in __init__
    response = lookup_resource(name)
  File "C:\Users\Harhit\AppData\Local\Programs\Python\Python36\lib\site-packages\pokebase\api.py", line 191, in lookup_resource
    with open('resource.json', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'resource.json'

FireFeathers06 avatar Jun 25 '18 08:06 FireFeathers06

I am also experiencing this issue.

I am using a Raspberry Pi, running Raspbian 2018-06-27.

Installed pokebase using:

sudo pip3 install pokebase

Running:

from pokebase import pokemon
pokemon("Charmander")

Produces the error:

Traceback (most recent call last):
  File "/home/pi/test_pokebase.py", line 2, in <module>
    pokemon("Charmander")
  File "/usr/local/lib/python3.5/dist-packages/pokebase/loaders.py", line 483, in pokemon
    return NamedAPIResource('pokemon', id_or_name)
  File "/usr/local/lib/python3.5/dist-packages/pokebase/api.py", line 311, in __init__
    n = APIResourceList(r).id_to_name(name)
  File "/usr/local/lib/python3.5/dist-packages/pokebase/api.py", line 395, in __init__
    response = lookup_resource(name)
  File "/usr/local/lib/python3.5/dist-packages/pokebase/api.py", line 191, in lookup_resource
    with open('resource.json', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'resource.json'

image

martinohanlon avatar Jul 04 '18 08:07 martinohanlon

Ok, so I have done some investigation...

It seems that when pokebase first creates the cache something can fail (I couldn't replicate what fails) but as a result I had an incomplete cache structure missing, importantly for this error, the "resource.json" file.

Deleting the cache

rm -rf ~/.cache/pokebase

... and re-running pokebase resolved the error.

Looking at the codebase for v1.2 (3e4655c3925c1508f666668dd04b409c5a24c3b3) a fix would be to change the test in lookup_resource to see if the cache exists to look for the existence of the 'resource.json' file not just the directory:

e.g. from

if os.path.exists(name) and not force_reload:

to:

if os.path.exists(os.path.join(name, 'resource.json')) and not force_reload:

martinohanlon avatar Jul 04 '18 09:07 martinohanlon

Fyi - we (the Raspberry Pi foundation) use pokebase in 1 of our online courses (to inject some fun and download pokemon - thanks) and I have seen quite a few people feedback this issue so it suggests it is not a freak occurrence and something which happens with some frequency.

martinohanlon avatar Jul 04 '18 09:07 martinohanlon

If you experience this error, deleting the pokebase cache should resolve the issue.

In windows open a command prompt and run:

rmdir /s "%USERPROFILE%\.cache\pokebase"

In Linux (inc Raspberry Pi), macOs open a terminal and run:

rm -rf ~/.cache/pokebase/

martinohanlon avatar Jul 04 '18 09:07 martinohanlon

Thanks @martinohanlon !

I would implement your code fix immediately, but the Pokebase structure has changed dramatically in the past week. There's still a bit more to do, but I'm gearing up for a 2.0 release. Much of the backend has been changed, for easier maintenance, but the biggest thing is that pokebase now uses shelve to store and cache the data, to avoid all the awkward directory and file creation. 1 cache, 1 file.

The source (Github) version is behind the one on PyPI because I haven't yet finished my testing improvements for the module, and along with the fact that Python 2 support should (probably) be added back in.

So please keep an eye out for 2.0 release, when this issue will no longer continue.

GregHilmes avatar Jul 04 '18 13:07 GregHilmes

Thanks @martinohanlon That works yay!

FireFeathers06 avatar Jun 11 '19 11:06 FireFeathers06

On windows 10 I am experiencing the same error:

FileNotFoundError: [Errno 2] No such file or directory: 'resource.json'

When I try to run code similar to:

from pokebase import pokemon pokemon("Charmander")

I have tried to delete the cache as per martinohanlon suggested, and have also uninstalled/reinstalled. Neither fix is working for me.

gabejchambers avatar Dec 22 '19 20:12 gabejchambers