'Unable to use Spotify credentials for predicting songs

I entered the Spotify credentials in the following code but unable to use those credentials and I don't know how to deal with this error. Any help would be appreciated.

from spotipy.oauth2 import SpotifyClientCredentials
from collections import defaultdict

sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=os.environ["SPOTIFY_CLIENT_ID"],
                                                       client_secret=os.environ["SPOTIFY_CLIENT_SECRET"]))


def find_song(name, year):

song_data = defaultdict()
results = sp.search(q= 'track: {} year: {}'.format(name,
                                                   year), limit=1)
if results['tracks']['items'] == []:
    return None

results = results['tracks']['items'][0]

track_id = results['id']
audio_features = sp.audio_features(track_id)[0]

song_data['name'] = [name]
song_data['year'] = [year]
song_data['explicit'] = [int(results['explicit'])]
song_data['duration_ms'] = [results['duration_ms']]
song_data['popularity'] = [results['popularity']]

for key, value in audio_features.items():
    song_data[key] = value

return pd.DataFrame(song_data)

KeyError: 'SPOTIFY_CLIENT_ID'



Solution 1:[1]

It seems that "SPOTIFY_CLIENT_ID" and probably "SPOTIFY_CLIENT_SECRET" too were not found in your environment variables.

Either you failed to add these variables to your user environment or python used a different user environment.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shravya Boggarapu