Fix for yfinance > 0.2.28 on Community Cloud -- /home/appuser/.cache/py-finance FileNotFound

I’ve seen a couple of posts related to yfinance not working recently.

NEW FIX: A fixed version of yfinance has been released on 2023-10-01

yfinance>=0.2.31b2

OLD FIX:

The actual error in the logs is FileNotFoundError: [Errno 2] No such file or directory: '/home/appuser/.cache/py-yfinance'

This is due to a a behavior change in the yfinance package that tries to write to a cache directory which doesn’t exist on Community Cloud.

You can either fix this by downgrading to yfinance==0.2.28, or with the hacky solution described here: GitHub - blackary/yf-fix: Fixing yfinance > 0.2.28 on Streamlit Community Cloud

from pathlib import Path

import appdirs as ad

CACHE_DIR = ".cache"

# Force appdirs to say that the cache dir is .cache
ad.user_cache_dir = lambda *args: CACHE_DIR

# Create the cache dir if it doesn't exist
Path(CACHE_DIR).mkdir(exist_ok=True)

import yfinance as yf

Note that you MUST run those other commands before importing yfinance, or it will fail. I believe this behavior should be considered a bug, and have reported it here: Importing yf fails if ~/.cache directory doesn't exist · Issue #1700 · ranaroussi/yfinance · GitHub

Here’s the proof-of-concept app

3 Likes

Thank you very much it works fine :slight_smile:

Thanks a lot @blackary . It saved me! :slight_smile: