How-To Guide
How to Install Pytube in Python
Pytube is a lightweight Python library for downloading YouTube videos. Here's how to install it — and how to fix the errors that usually follow.
⚠️ Before you install: Pytube is intended for downloading videos you have the right to download — your own content, public-domain material, or content licensed for reuse. Downloading copyrighted videos without permission violates YouTube's Terms of Service. Use responsibly.
📦 What is Pytube?
Pytube is a pure-Python library (no third-party dependencies) that lets you interact with YouTube programmatically. It's popular for:
- Downloading videos for offline viewing
- Extracting video metadata (title, length, thumbnails)
- Downloading only the audio track
- Selecting specific resolutions or formats
- Building download tools and automation scripts
The package is available on PyPI as pytube. However, the original project has had maintenance gaps — which is why this guide also covers pytubefix, a maintained fork that stays compatible with YouTube's frequent changes.
📋 What You'll Need
- Python 3.8 or newer installed
- pip (Python's package manager, included with Python 3.4+)
- An internet connection
- A terminal (Command Prompt, PowerShell, or Terminal)
- About 3 minutes of your time
Not sure if Python is installed? Read our guide: How to Install Python →
Verify Python and pip are installed
Open your terminal and run:
python --version
pip --version
You should see something like:
Python 3.12.1
pip 24.0 from ... (python 3.12)
💡 On Mac/Linux: You may need to use python3 and pip3 instead of python and pip.
Install Pytube
Run this in your terminal:
pip install pytube
If you get a permission error, use:
pip install --user pytube
Or, for the maintained fork (recommended if you run into HTTP 403 errors):
pip install pytubefix
💡 Mac / Linux: If pip isn't found, try pip3 install pytube. If you're using a virtual environment (recommended), activate it first.
Verify the installation
Run this one-liner to confirm Pytube is installed:
python -c "import pytube; print(pytube.__version__)"
If you installed pytubefix instead:
python -c "import pytubefix; print(pytubefix.__version__)"
You should see a version number — for example, 15.0.0. If you see an error, jump to the "Common Errors" section below.
Test with a simple script
Create a new file called downloader.py and paste this:
from pytube import YouTube
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
yt = YouTube(url)
print("Title:", yt.title)
print("Length:", yt.length, "seconds")
print("Author:", yt.author)
# Download highest resolution video
yt.streams.get_highest_resolution().download()
print("Download complete!")
Run it with:
python downloader.py
If a video file appears in your folder, installation is successful. 🎉
🎵 Bonus: Download Only the Audio
To download just the audio track (useful for music or podcasts):
from pytube import YouTube
yt = YouTube("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
audio = yt.streams.filter(only_audio=True).first()
audio.download()
# Optional: rename .mp4 to .mp3 after downloading
print("Audio downloaded!")
Pytube saves the audio as an .mp4 file by default. You can rename it to .mp3 afterward, or use FFmpeg to convert it properly.
🔧 Common Errors (and Fixes)
❌ HTTP Error 403: Forbidden
YouTube changed its API and the original Pytube stopped working. Fix:
pip uninstall pytube
pip install pytubefix
Then change your import from from pytube import YouTube to from pytubefix import YouTube.
❌ ModuleNotFoundError: No module named 'pytube'
This means you installed it in a different Python environment. Try:
python -m pip install pytube
❌ 'pip' is not recognized as an internal or external command
Python isn't on your system PATH. Reinstall Python and check "Add Python to PATH" on the first installer screen. Or, use:
python -m pip install pytube
❌ PytubeExtractError / RegexMatchError
YouTube changed its HTML structure. This is why switching to pytubefix is often the quickest fix:
pip uninstall pytube -y
pip install pytubefix
❌ AttributeError: 'NoneType' object has no attribute 'download'
The stream you selected doesn't exist for that video (e.g., no 1080p available). Always filter with a fallback:
stream = yt.streams.get_highest_resolution()
if stream:
stream.download()
else:
print("No suitable stream found.")
⚖️ Pytube vs pytubefix: Which Should You Use?
Since 2024, YouTube has made frequent changes that break the original Pytube. Here's a quick comparison:
pytube
- Original package on PyPI
- Widely documented
- ⚠️ Often breaks with new YouTube changes
- ⚠️ Slow updates
pytubefix ✅
- Actively maintained fork
- Same API as pytube
- Regular compatibility updates
- Recommended for new projects
💡 Rule of thumb: If you're learning or building a small script, use pytubefix for fewer headaches. If your project already uses pytube and works, don't change it — just be aware it may break in the future.
🎯 What's Next?
Now that you can download videos with Python, level up your skills: