Sphinx YouTube Extension
sphinxcontrib-youtube is a Sphinx extension that enables embedding YouTube, Vimeo, and PeerTube videos directly into reStructuredText or MyST documentation. It is currently at version 1.5.0 and is actively maintained with several releases per year addressing compatibility with Sphinx, adding new features, and bug fixes.
Common errors
-
Sphinx error: unknown extension: 'youtube'
cause The extension name was incorrectly specified in `conf.py`.fixChange `extensions = ['youtube']` to `extensions = ['sphinxcontrib.youtube']` in your `conf.py` file. -
ERROR: Could not find a version that satisfies the requirement sphinxcontrib-youtube (from versions: none) ERROR: No matching distribution found for sphinxcontrib-youtube
cause Typo in the package name during `pip install` or PyPI is unreachable.fixVerify the package name: `pip install sphinxcontrib-youtube`. Ensure your internet connection is active. -
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
cause This error or similar Python syntax errors can occur if you are running a `sphinxcontrib-youtube` version that uses newer Python features (e.g., f-strings) on an older Python environment (like Python 3.5).fixUpgrade your Python environment to 3.6 or newer. Refer to the `deprecated` warning regarding Python 3.5 support. -
Video does not appear in the generated HTML/PDF output.
cause This can be due to an incorrect or malformed video URL, network issues preventing thumbnail download for LaTeX, or JavaScript blockers in the browser for HTML.fix1. Check the video URL for correctness (must be full `https://` URL). 2. Inspect your browser's developer console for JavaScript errors. 3. Ensure your build environment has network access if generating LaTeX/PDF output.
Warnings
- gotcha When adding the extension to `conf.py`, ensure you use the full dotted name `sphinxcontrib.youtube`, not just `youtube`.
- deprecated Python 3.5 is no longer supported. Version 1.0.1 was released specifically to support Python 3.5 by removing f-strings, but subsequent versions have moved to maintain compatibility with actively supported Python versions (typically Python 3.6+).
- gotcha When building LaTeX output, `sphinxcontrib-youtube` downloads video thumbnails. This process requires network access and can increase build times. If your build environment is offline or has strict network policies, LaTeX builds might fail or be significantly slower.
- gotcha Video URLs must be complete, including the `http://` or `https://` scheme. Relative paths or IDs without a full URL scheme may not render correctly.
Install
-
pip install sphinxcontrib-youtube
Imports
- sphinxcontrib.youtube
extensions = ['youtube']
extensions = ['sphinxcontrib.youtube']
Quickstart
# conf.py
import os
import sys
project = 'My Sphinx Project'
copyright = '2023, Your Name'
extensions = [
'sphinxcontrib.youtube',
]
# html_theme = 'alabaster' # Or any other Sphinx theme
# In your .rst or .md file (e.g., index.rst):
#
# .. youtube:: https://www.youtube.com/watch?v=dQw4w9WgXcQ
# :width: 560
# :height: 315
# :align: center
# :alt: Example YouTube Video
#
# For Vimeo or PeerTube, use their respective full URLs.
# .. youtube:: https://vimeo.com/209761509
# :width: 560
# :height: 315