Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a standard location to store function cache files in Python?

I have these scripts that perform a lot of expensive computations. One optimization I do is to evaluate the expensive function on a grid, cache the grid, and interpolate for calls within the grid. It is important that the files be written out to disk so that future sessions can benefit from the cache. When this script was just for my own personal use, I could put the cache wherever. Now that I want to incorporate it into a package for wider distribution, I find that I need to have some way to know where is it kosher to store cache files for a package? Do I just need to make some decisions, or are there some procedures I should follow (e.g. let the user modify the directory at install time, at run-time, etc)?

Edit: this code will also have functions based on interpolating data from other sources. So I will need a place to store those, too, but need to know where it is standard to do so and how to detect where that is on a particular install (something hard coded at install time, or can Python modules detect where they're installed at runtime?). Point being, this location needs to be persistent in a way that I understand temporary directories not to be. Therefore, this would ideally be done inside of the package's directory structure or somewhere in the user's home directory. If in the package's directories, it is acceptable if I have to do shenanigans with permissions to make a directory that the user can modify.

like image 580
Sean Lake Avatar asked Apr 22 '26 11:04

Sean Lake


2 Answers

I have the same requirements.

It seems like the way to go is storing to ~/.cache/<app name> (Linux).

For covering different operating systems, getting similar location can be simplified with platformdirs (src, PyPI, conda-forge), e.g.:

from platformdirs import user_cache_dir
cachedir = user_cache_dir("<app name>", "<app author>")
like image 135
Danila Vershinin Avatar answered Apr 24 '26 00:04

Danila Vershinin


I found some cached files here C:\Users\User_Name\AppData\Local\pip\cache

It is the folder where pip is installed and cache files/libraries are present.

If you uninstall and reinstall python, then these cached libraries will be used, instead of downloading new ones.

like image 28
Akarshit Sharma Avatar answered Apr 23 '26 23:04

Akarshit Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!