PlexAPI
PlexAPI is a Python library that provides bindings for the Plex Media Server API. It allows programmatic interaction with your Plex server, enabling tasks such as managing libraries, controlling media playback, retrieving metadata, and more. The library is actively maintained, with frequent releases, typically every few weeks.
Warnings
- breaking PlexAPI now requires Python 3.10 or newer. Earlier Python versions are no longer supported.
- breaking All previously deprecated methods have been removed.
- breaking The `authToken` attribute has been removed from local server account objects. Direct access to this attribute is no longer available.
- breaking The `Hub.items` property has been changed to a `Hub.items()` method.
- gotcha Accessing `MediaPart.exists` and `MediaPart.accessible` attributes now requires explicitly reloading the `MediaPart` object with `checkFiles=True` to ensure up-to-date status.
Install
-
pip install plexapi
Imports
- PlexServer
from plexapi.server import PlexServer
Quickstart
import os
from plexapi.server import PlexServer
# Replace with your Plex URL and token, or set as environment variables
# Example: PLEX_URL='http://your_plex_ip:32400', PLEX_TOKEN='YOUR_PLEX_TOKEN'
PLEX_URL = os.environ.get('PLEX_URL', 'http://localhost:32400') # Default for local access
PLEX_TOKEN = os.environ.get('PLEX_TOKEN', 'YOUR_PLEX_TOKEN') # Get from Plex settings > Network > Show Advanced > token
try:
plex = PlexServer(PLEX_URL, PLEX_TOKEN)
print(f"Connected to Plex server: {plex.friendlyName}")
# List all libraries
print("\nLibraries:")
for section in plex.library.sections():
print(f"- {section.title} ({section.type})")
# Example: Find a movie in a 'Movies' library
# Adjust 'Movies' to the actual name of your movie library
try:
movies_library = plex.library.section('Movies')
inception_movie = movies_library.get('Inception')
print(f"\nFound movie: {inception_movie.title} ({inception_movie.year})")
except Exception as e:
print(f"\nCould not find 'Movies' library or 'Inception': {e}")
except Exception as e:
print(f"Error connecting to Plex or performing operations: {e}")
print("Please ensure PLEX_URL and PLEX_TOKEN are correctly configured in environment variables or directly in the script.")