Python YouTube Data API Wrapper
A Python wrapper around for YouTube Data API. It provides an easy way to interact with YouTube Data API V3, covering all resource methods like `insert` and `update`. The library is actively developed, with version 0.9.8 released on August 22, 2025, and maintains a consistent release cadence with several updates in 2024 and 2025.
Common errors
-
ModuleNotFoundError: No module named 'pyyoutube'
cause The `python-youtube` package or its internal `pyyoutube` module is not installed in the current Python environment.fixInstall the library using pip: `pip install python-youtube`. -
pyyoutube.exceptions.PyYouTubeException: The request is missing a required API key.
cause The `api_key` parameter was either not provided to the `Client` constructor or the provided key is invalid/restricted. Also, the YouTube Data API v3 might not be enabled for your Google Cloud project.fixObtain a valid API key from the Google Cloud Console, enable the YouTube Data API v3 for your project, and pass it correctly to `Client(api_key="YOUR_API_KEY")`. -
pyyoutube.exceptions.PyYouTubeException: The request is not authorized.
cause You are attempting an operation that requires user authorization (OAuth 2.0 access token) with an invalid, expired, or missing access token, or with only an API key.fixFor operations requiring user consent, you must implement the OAuth 2.0 flow to acquire and refresh access tokens. Initialize the client with `Client(access_token='YOUR_ACCESS_TOKEN')` for authorized requests. -
AttributeError: 'ChannelsResource' object has no attribute 'get_channel_info'
cause You are trying to use an older method (e.g., `get_channel_info`) from the deprecated `pyyoutube.Api` class on an instance of the newer `pyyoutube.Client` class, which uses a different, structured interface.fixRefer to the latest documentation for `pyyoutube.Client`. For fetching channel info, use `client.channels.list(channel_id=...)` or similar structured calls instead.
Warnings
- deprecated The `pyyoutube.Api` class represents an older client structure. While still supported for backward compatibility, the `pyyoutube.Client` class is the recommended and more feature-rich approach for interacting with the YouTube Data API.
- deprecated As of version 0.9.2, some parameters or methods within the library have been marked as deprecated. Specific details are not always highlighted in release notes, but users should consult the latest official documentation to avoid using deprecated functionality.
- gotcha Authentication for the YouTube Data API requires careful handling of API keys for public data access and OAuth 2.0 for user-authorized actions. Misconfiguring these can lead to authorization errors or unexpected behavior.
Install
-
pip install python-youtube
Imports
- Client
from python_youtube import Client
from pyyoutube import Client
Quickstart
import os
from pyyoutube import Client
# Get your API key from Google Cloud Console and enable YouTube Data API v3
# Set it as an environment variable or replace 'YOUR_API_KEY'
API_KEY = os.environ.get('YOUTUBE_API_KEY', 'YOUR_API_KEY')
if API_KEY == 'YOUR_API_KEY':
print("Warning: Please set the YOUTUBE_API_KEY environment variable or replace 'YOUR_API_KEY' with your actual YouTube Data API Key.")
else:
try:
client = Client(api_key=API_KEY)
# Fetch information about a public YouTube channel (e.g., GoogleDevelopers)
channel_by_id = client.channels.list(channel_id="UC_x5XG1OV2P6uZZ5FSM9Ttw")
if channel_by_id.items:
channel_title = channel_by_id.items[0].snippet.title
print(f"Channel Title: {channel_title}")
else:
print("Channel not found or API key is invalid.")
except Exception as e:
print(f"An error occurred: {e}")