pylast

raw JSON →
7.0.2 verified Mon Apr 27 auth: no python

A Python interface to Last.fm and Libre.fm. Current version 7.0.2 (requires Python >=3.10). Uses httpx for HTTP requests. Active development; releases every few months.

pip install pylast
error AttributeError: module 'pylast' has no attribute 'LastFMNetwork'
cause Using `import pylast` and then `pylast.LastFMNetwork` fails because LastFMNetwork is a class, not a module-level attribute. The class is not exposed at the top-level by default.
fix
Use from pylast import LastFMNetwork.
error pylast.WSError: Invalid API Key
cause You provided an API key that is not valid or not active for Last.fm.
fix
Register at https://www.last.fm/api/account/create and use that key. Double-check you are using the correct key and secret.
error httpx.ConnectError: All connection attempts failed
cause Network issue, proxy problem, or DNS failure. Also can happen if proxy configuration is wrong.
fix
Check your internet connection. If using a proxy, pass it correctly: LastFMNetwork(api_key, api_secret, proxy="http://proxy:8080") or as a dict for multiple proxies.
error TypeError: 'str' object cannot be interpreted as an integer
cause Passing a string where integer expected, e.g., `limit="10"` instead of `limit=10`.
fix
Ensure numeric parameters are integers, e.g., limit=10.
error pylast.WSError: The service is temporarily unavailable. Please try again.
cause Last.fm API rate limiting or temporary outage.
fix
Wait a few seconds and retry the request. Implement exponential backoff in your code.
breaking In version 5.0.0, the HTTP library changed from `http.client` to `httpx`. Custom session handling or proxies must be adapted.
fix If you were using `http.client` or relying on `urllib3` behavior, you must migrate to httpx proxy configuration.
breaking In version 6.0.0, proxy support was restored but the `network.proxy` attribute is now always a `dict` (previously could be `str` or `dict`).
fix Check for `isinstance(network.proxy, dict)` instead of expecting a string.
breaking In version 7.0.0, `SCROBBLE_SOURCE_*` and `SCROBBLE_MODE_*` constants were removed. Use `chosen_by_user` parameter instead.
fix Replace `SCROBBLE_SOURCE_USER` etc. with the `chosen_by_user` boolean parameter in `scrobble` calls.
gotcha The `get_top_tracks()` and similar methods return `TopItem` objects, not `Track` objects. Access the actual track via `.item`.
fix Use `for top_item in user.get_top_tracks(): track = top_item.item`.
gotcha When using session key authentication, you must call `network.get_session_key()` with the token first. Many users forget this step.
fix See the authentication example in the README: generate token, get session key, then create `LastFMNetwork(api_key, api_secret, session_key)`.
deprecated In version 5.0.0, `is_streamable` and `is_fulltrack_available` were removed.
fix Use the `streamable` attribute on Track objects instead.

Initialize LastFMNetwork with API credentials and fetch top tracks for a user.

from pylast import LastFMNetwork

api_key = os.environ.get('LASTFM_API_KEY', '')
api_secret = os.environ.get('LASTFM_API_SECRET', '')
network = LastFMNetwork(api_key=api_key, api_secret=api_secret)

# Get top tracks for a user
try:
    user = network.get_user('RJ')
    top_tracks = user.get_top_tracks(limit=3)
    for track in top_tracks:
        print(track.item)
except Exception as e:
    print(f"Error: {e}")