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

ImportError using yf.Ticker #1751

Closed
Philip030601 opened this issue Nov 29, 2023 · 9 comments
Closed

ImportError using yf.Ticker #1751

Philip030601 opened this issue Nov 29, 2023 · 9 comments

Comments

@Philip030601
Copy link

Hello. Hope everyone is doing well. Getting an error after upgraded yfinance today (11/28/23) with the following code %pip install yfinance --upgrade --no-cache-dir --pre.

I am using python version 3.10 and yfinance version 02.32

Error I am getting is as follows:

ImportError: cannot import name 'DO_NOT_CACHE' from 'requests_cache' (C:\Users\17147\anaconda3\envs\ib230729\Lib\site-packages\requests_cache_init_.py)

Simple code that reproduces the problem:
session = requests_cache.CachedSession('yfinance.cache')
session.headers['User-agent'] = 'my-program/1.0'
msft = yf.Ticker('msft', session=session)
msft.actions

Traceback below:

ImportError Traceback (most recent call last)
Cell In[2], line 3
1 session = requests_cache.CachedSession('yfinance.cache')
2 session.headers['User-agent'] = 'my-program/1.0'
----> 3 msft = yf.Ticker('msft', session=session)

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\ticker.py:34, in Ticker.init(self, ticker, session)
33 def init(self, ticker, session=None):
---> 34 super(Ticker, self).init(ticker, session=session)
35 self._expirations = {}
36 self._underlying = {}

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\base.py:71, in TickerBase.init(self, ticker, session)
68 if utils.is_isin(self.ticker):
69 self.ticker = utils.get_ticker_by_isin(self.ticker, None, session)
---> 71 self._data: YfData = YfData(session=session)
73 self._analysis = Analysis(self._data, ticker)
74 self._holders = Holders(self._data, ticker)

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\data.py:47, in SingletonMeta.call(cls, *args, **kwargs)
45 with cls._lock:
46 if cls not in cls._instances:
---> 47 instance = super().call(*args, **kwargs)
48 cls._instances[cls] = instance
49 else:

File ~\anaconda3\envs\ib230729\Lib\site-packages\yfinance\data.py:75, in YfData.init(self, session)
70 else:
71 # Is caching. This is annoying.
72 # Can't simply use a non-caching session to fetch cookie & crumb,
73 # because then the caching-session won't have cookie.
74 self._session_is_caching = True
---> 75 from requests_cache import DO_NOT_CACHE
76 self._expire_after = DO_NOT_CACHE
77 self._crumb = None

ImportError: cannot import name 'DO_NOT_CACHE' from 'requests_cache' (C:\Users\17147\anaconda3\envs\ib230729\Lib\site-packages\requests_cache_init_.py)

@ValueRaider
Copy link
Collaborator

What version requests_cache?

@Philip030601
Copy link
Author

What version requests_cache?

0.4.12

@ValueRaider
Copy link
Collaborator

DO_NOT_CACHE must be a new feature so fix = update. Because requests_cache is optional I'm not sure how to enforce minimum version.

@Philip030601
Copy link
Author

DO_NOT_CACHE must be a new feature so fix = update. Because requests_cache is optional I'm not sure how to enforce minimum version.

Should i download a prior version that worked? If so, which one do u recommend? Thanks

@bot-unit
Copy link

bot-unit commented Nov 30, 2023

pip install requests-cache==1.1.1 yfinance==0.2.32 --upgrade --no-cache-dir
in your case:
conda update requests-cache yfinance
or just remove reuqests-cache if you don't use it

@bot-unit
Copy link

bot-unit commented Dec 2, 2023

@ValueRaider
according documentation
at the file data.py on the line 200:

if self._session_is_caching:
    # get_args['expire_after'] = self._expire_after
    with self._session.cache_disabled():
        crumb_response = self._session.get(**get_args)

so you don't need import at lines 75-76

@ValueRaider
Copy link
Collaborator

ValueRaider commented Dec 3, 2023

@bot-unit
Copy link

bot-unit commented Dec 3, 2023

So maybe use
DO_NOT_CACHE = 0x0D0E0200020704 # Per RFC 4824
instead import it

@ValueRaider
Copy link
Collaborator

ValueRaider commented Dec 23, 2023

Fix will be setting minimum version of requests_cache, possible by #1771

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

3 participants