barnumbirr / coinmarketcap

A python wrapper around the https://coinmarketcap.com API.
Apache License 2.0
436 stars 109 forks source link

AttributeError: module 'requests_cache' has no attribute 'core' #58

Open haydencall opened 3 years ago

haydencall commented 3 years ago

The request sent doesn't have the attribute 'core' Has anyone else had this issue? What version of requests should I have imported to use this library?

JWCook commented 3 years ago

Looks like it's pinned to 0.4.13 in requirements.txt.

To use the latest version, I think this line here: https://github.com/barnumbirr/coinmarketcap/blob/2b951be7cedd7efc484f50f27600ee8a003f3c94/coinmarketcap/core.py#L27

Just needs to reference requests_cache.CachedSession instead of requests_cache.core.CachedSession.

tifDev commented 3 years ago

Same error, what is the solution for this? I executed that line but gave other error:

>>> self._session = requests_cache.core.CachedSession(cache_name=self.cache_name, backend='sqlite', expire_after=120) 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'requests_cache' is not defined
JWCook commented 3 years ago

That looks like a different error, probably just missing dependencies. Try:

pip install requests-cache==0.5.2
tifDev commented 3 years ago

No Need. I managed to make it work this morning:


from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import pprint 
import os

pp = pprint.PrettyPrinter(indent=4)
key = yourAPIkey

url = f'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
print(url)
headers = {
    'Accepts': 'application/json',
    'X-CMC_PRO_API_KEY': key,
}
parameters = {
'symbol':'BTC,ETH'

}

session = Session()
session.headers.update(headers)

try:
    response = session.get(url, params=parameters)
    data = json.loads(response.text)
    btcPrice = data["data"]["BTC"]["quote"]["USD"]["price"]

    pp.pprint(btcPrice)

except (ConnectionError, Timeout, TooManyRedirects) as e:
    data = json.loads(response.text)
    pp.pprint(data)
aoki-h-jp commented 1 year ago

@tifDev This works for me, Thanks!