[GH-ISSUE #579] Getting error 'Couldn't read cache at: .cache' with example code #348

Closed
opened 2026-02-27 23:22:07 +03:00 by kerem · 11 comments
Owner

Originally created by @kbots-tech on GitHub (Sep 30, 2020).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/579

Describe the bug
Using the example auth code I get the error 'Couldn't read cache at: .cache' I've tried looking around but can't find a reference to this specific error anywhere. I'm using the Online IDE repl.it but it seems users have used it with no trouble before looking at other projects

Your code
import spotipy
from spotipy.oauth2 import SpotifyOAuth

scope = "user-library-read"

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))

results = sp.current_user_saved_tracks()
for idx, item in enumerate(results['items']):
track = item['track']
print(idx, track['artists'][0]['name'], " – ", track['name'])

Environment:

  • OS: [Linux Container (I believe)]
  • Python version [e.g. 3.8.2]
  • spotipy version [e.g. 2.16.0]
  • your IDE (if using any) [Repl.it]
Originally created by @kbots-tech on GitHub (Sep 30, 2020). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/579 **Describe the bug** Using the example auth code I get the error 'Couldn't read cache at: .cache' I've tried looking around but can't find a reference to this specific error anywhere. I'm using the Online IDE repl.it but it seems users have used it with no trouble before looking at other projects **Your code** import spotipy from spotipy.oauth2 import SpotifyOAuth scope = "user-library-read" sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope)) results = sp.current_user_saved_tracks() for idx, item in enumerate(results['items']): track = item['track'] print(idx, track['artists'][0]['name'], " – ", track['name']) **Environment:** - OS: [Linux Container (I believe)] - Python version [e.g. 3.8.2] - spotipy version [e.g. 2.16.0] - your IDE (if using any) [Repl.it] *
kerem 2026-02-27 23:22:07 +03:00
Author
Owner

@Quizz1Cal commented on GitHub (Oct 3, 2020):

(note: I am a newbie to open-source contributing, feedback on my correctness would be appreciated)

This is expected behaviour for (initial) runs of Spotipy as far the current version allows. Line 310 of spotipy/oauth2.py illustrates that Spotipy throws this warning if a cache file for tokens has not yet been instantiated. This is a relatively new feature (see #567) to allow explicit specification of cache files to be optional, hence probably why you can't find other documentation.

This shouldn't prevent your code from working. On repeat executions, the error goes away regardless (unless you keep changing the working directory...!); otherwise, I guess if you instantiate the file .cache in the working directory prior to using SpotifyOAuth() you can avoid the warning, but this feels impractical. Hope that helps!

<!-- gh-comment-id:703047520 --> @Quizz1Cal commented on GitHub (Oct 3, 2020): (note: I am a newbie to open-source contributing, feedback on my correctness would be appreciated) This is expected behaviour for (initial) runs of Spotipy as far the current version allows. Line 310 of [spotipy/oauth2.py](https://github.com/plamere/spotipy/blob/master/spotipy/oauth2.py#L310) illustrates that Spotipy throws this warning if a cache file for tokens has not yet been instantiated. This is a relatively new feature (see #567) to allow explicit specification of cache files to be optional, hence probably why you can't find other documentation. This shouldn't prevent your code from working. On repeat executions, the error goes away regardless (unless you keep changing the working directory...!); otherwise, I guess if you instantiate the file `.cache` in the working directory prior to using `SpotifyOAuth()` you can avoid the warning, but this feels impractical. Hope that helps!
Author
Owner

@stephanebruckert commented on GitHub (Oct 3, 2020):

This is true @Quizz1Cal, it is just a warning so don't worry about it. Probably that your current environment (repl.it) doesn't allow storing new files. Can you open an issue on their side to ask if this is expected?

In the meantime, let us know if this issue prevents you from using any other features

<!-- gh-comment-id:703083419 --> @stephanebruckert commented on GitHub (Oct 3, 2020): This is true @Quizz1Cal, it is just a warning so don't worry about it. Probably that your current environment (repl.it) doesn't allow storing new files. Can you open an issue on their side to ask if this is expected? In the meantime, let us know if this issue prevents you from using any other features
Author
Owner

@kbots-tech commented on GitHub (Oct 4, 2020):

That may be it, I ended up manually doing the oauth which took a bit more time but ended up working, thanks. It did continue on every run though so I was unable to use this library. Thanks for making it though if it worked in my enviroment it definitely would have been a great help but not the fault of you then but on their end.

<!-- gh-comment-id:703286857 --> @kbots-tech commented on GitHub (Oct 4, 2020): That may be it, I ended up manually doing the oauth which took a bit more time but ended up working, thanks. It did continue on every run though so I was unable to use this library. Thanks for making it though if it worked in my enviroment it definitely would have been a great help but not the fault of you then but on their end.
Author
Owner

@feifeinew commented on GitHub (Jan 21, 2021):

I had the exact same question! I still didn't figure it out. Any solution would be appreciated.

<!-- gh-comment-id:764259148 --> @feifeinew commented on GitHub (Jan 21, 2021): I had the exact same question! I still didn't figure it out. Any solution would be appreciated.
Author
Owner

@PrynsTag commented on GitHub (Jul 16, 2023):

I had the exact same question! I still didn't figure it out. Any solution would be appreciated.

Got the same issue. Just run it in sudo (linux) or Administrator (windows) and it will create .cache file the same directory it was run.

<!-- gh-comment-id:1637042216 --> @PrynsTag commented on GitHub (Jul 16, 2023): > I had the exact same question! I still didn't figure it out. Any solution would be appreciated. Got the same issue. Just run it in sudo (linux) or Administrator (windows) and it will create .cache file the same directory it was run.
Author
Owner

@dieser-niko commented on GitHub (Jul 16, 2023):

Probably not a very good idea. It certainly works, but using admin perms to just run a simple script is not recommended.

<!-- gh-comment-id:1637159982 --> @dieser-niko commented on GitHub (Jul 16, 2023): Probably not a very good idea. It certainly works, but using admin perms to just run a simple script is not recommended.
Author
Owner

@WombatCombatMaciej commented on GitHub (Aug 19, 2023):

Hey! I know the problem is 2 years old but I solved it on my Raspberry PI. The problem was folder permission! Working directly in home folder is not good idea :S

<!-- gh-comment-id:1685082191 --> @WombatCombatMaciej commented on GitHub (Aug 19, 2023): Hey! I know the problem is 2 years old but I solved it on my Raspberry PI. The problem was folder permission! Working directly in home folder is not good idea :S
Author
Owner

@andystumpf commented on GitHub (Jan 18, 2024):

This worked for me

sudo chmod -R u+w /root/.spotdl/.spotipy

<!-- gh-comment-id:1897577961 --> @andystumpf commented on GitHub (Jan 18, 2024): This worked for me `sudo chmod -R u+w /root/.spotdl/.spotipy`
Author
Owner

@jonathancaudill commented on GitHub (Jun 22, 2024):

Hey! I know the problem is 2 years old but I solved it on my Raspberry PI. The problem was folder permission! Working directly in home folder is not good idea :S

can you please elaborate? stuck here as well. the .cache file exists for me too...

<!-- gh-comment-id:2183827526 --> @jonathancaudill commented on GitHub (Jun 22, 2024): > Hey! I know the problem is 2 years old but I solved it on my Raspberry PI. The problem was folder permission! Working directly in home folder is not good idea :S can you please elaborate? stuck here as well. the .cache file exists for me too...
Author
Owner

@bwareham commented on GitHub (Dec 18, 2024):

TL;DR try substituting the MemoryCacheHandler for the default CacheFileHandler

Like others, I ran into the the read/write cache errors. In my case, I was using Spotipy from a Django management command. When I ran it from via manage.py inside the project folder it worked fine, but when I scheduled it as a cronjob I'd get the cache errors. Mostly it was just an annoyance -- hundreds of error messages in the log every time the command ran. But it did occasionally prevent execution of the routine.

The problem appeared to be that cron by default runs from a root folder that I don't have sufficient permissions for on my shared server.

After reading a whole lot of suggested fixes I first tried to force cron to run the command from the project folder. When I couldn't make that work, I tried using the DjangoSessionCacheHandler. After fumbling around with the code, I finally got it installed, but got an error saying it required a position argument. I assume it was telling me I needed to configure the session middleware somehow, but that was outside the scope of my knowledge. (Someone who knows better than me can fill in details here.)

I then tried the MemoryCacheHandler and, so far, it has worked like a charm. Here's the relevant code:

from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.cache_handler import MemoryCacheHandler 

auth_manager = SpotifyClientCredentials(cache_handler=MemoryCacheHandler())
sp = spotipy.Spotify(auth_manager=auth_manager)

Hope this helps others who have encountered this problem.

<!-- gh-comment-id:2551977365 --> @bwareham commented on GitHub (Dec 18, 2024): TL;DR try substituting the MemoryCacheHandler for the default CacheFileHandler Like others, I ran into the the read/write cache errors. In my case, I was using Spotipy from a Django management command. When I ran it from via manage.py inside the project folder it worked fine, but when I scheduled it as a cronjob I'd get the cache errors. Mostly it was just an annoyance -- hundreds of error messages in the log every time the command ran. But it did occasionally prevent execution of the routine. The problem appeared to be that cron by default runs from a root folder that I don't have sufficient permissions for on my shared server. After reading a whole lot of suggested fixes I first tried to force cron to run the command from the project folder. When I couldn't make that work, I tried using the DjangoSessionCacheHandler. After fumbling around with the code, I finally got it installed, but got an error saying it required a position argument. I assume it was telling me I needed to configure the session middleware somehow, but that was outside the scope of my knowledge. (Someone who knows better than me can fill in details here.) I then tried the MemoryCacheHandler and, so far, it has worked like a charm. Here's the relevant code: ``` from spotipy.oauth2 import SpotifyClientCredentials from spotipy.cache_handler import MemoryCacheHandler auth_manager = SpotifyClientCredentials(cache_handler=MemoryCacheHandler()) sp = spotipy.Spotify(auth_manager=auth_manager) ``` Hope this helps others who have encountered this problem.
Author
Owner

@dieser-niko commented on GitHub (Dec 18, 2024):

Alternatively you can create your own cache handler to store the token in a database for example

<!-- gh-comment-id:2552010226 --> @dieser-niko commented on GitHub (Dec 18, 2024): Alternatively you can create your own cache handler to store the token in a database for example
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#348
No description provided.