Automate Spotify and YouTube Playlists – Chapter 5: Running It Locally

작성자

카테고리:

← 피드로
DEV Community · Tawanda Nyahuye · 2026-07-24 개발(SW)

This is the chapter where things get real. We’re going to set up Python, create the project folder, put all the pieces together, and run the script for the first time. By the end, you’ll have a working Spotify playlist populated with 70s hits.

Let’s go step by step.

Step 1: Install Python

Python is the programming language the script is written in. If you already have it installed, skip to Step 2.

On Windows

Go to python.org/downloads and click the big yellow Download Python button. Run the installer.

One thing — and this is important — on the first screen of the installer, before you click anything else, check the box that says “Add Python to PATH”. It’s at the bottom and it’s easy to miss. If you skip this, Python won’t work from the command line and you’ll spend an hour wondering why.

After that, just click through the defaults and let it install.

On Mac

Mac often comes with Python pre-installed, but it’s usually an old version. The cleanest way is to go to python.org/downloads, download the Mac installer, and run it. Follow the defaults.

On Linux

You almost certainly already have Python. Open a terminal and type:

python3 --version

Enter fullscreen mode Exit fullscreen mode

If you see a version number, you’re good. If not:

sudo apt install python3

Enter fullscreen mode Exit fullscreen mode

Verify the installation

Open a terminal (on Windows, search for “Command Prompt” or “PowerShell”). Type:

python --version

Enter fullscreen mode Exit fullscreen mode

You should see something like Python 3.12.4. Any version of Python 3 works. If you see Python 2.x.x, try python3 --version instead and use python3 everywhere in this chapter.

Step 2: Set Up the Project Folder

Create a folder somewhere on your computer. Call it youtube-to-spotify. Inside it, you need four files:

youtube-to-spotify/
├── spotify.py
├── youtube.py
├── script.py
└── .env

Enter fullscreen mode Exit fullscreen mode

The first three files are available in the youtube-to-spotify repo on GitHub. Download them and place them in your folder.

For the .env file, create a new text file in your folder, name it .env (just that, no other extension), and fill it in with your credentials from Chapters 2 and 3:

CLIENT_ID=your_spotify_client_id
CLIENT_SECRET=your_spotify_client_secret
REFRESH_TOKEN=your_spotify_refresh_token
SPOTIFY_PLAYLIST_ID=your_spotify_playlist_id
API_KEY=your_youtube_api_key
YOUTUBE_PLAYLIST_ID=PLGBuKfnErZlAkaUUy57-mR97f8SBgMNHh

Enter fullscreen mode Exit fullscreen mode

Save it. Don’t share it. Don’t commit it to GitHub — we’ll handle that in Chapter 6.

Windows users: Windows sometimes adds a .txt extension without telling you, turning .env into .env.txt. To check, open File Explorer, go to the View tab, and enable “File name extensions”. If your file shows as .env.txt, rename it and remove the .txt part.

Step 3: Open a Terminal in Your Project Folder

You’ll run all the commands in this chapter from inside your youtube-to-spotify folder.

On Windows: Open File Explorer, navigate to your project folder, click the address bar at the top, type cmd, and press Enter. A Command Prompt window opens already pointing at your folder.

On Mac: Open Terminal, then drag your project folder into the Terminal window and press Enter. Or type cd (with a space) and then drag the folder in.

On Linux: Right-click inside the folder and select “Open Terminal here”, or use cd to navigate there.

Confirm you’re in the right place by typing:

ls

Enter fullscreen mode Exit fullscreen mode

(On Windows, use dir instead.) You should see your four files listed.

Step 4: Create a Virtual Environment

A virtual environment is an isolated box for your project’s dependencies. It means the packages you install for this project don’t interfere with anything else on your computer, and vice versa.

You only do this once per project.

python -m venv venv

Enter fullscreen mode Exit fullscreen mode

This creates a folder called venv inside your project. Now activate it:

On Windows:

venv\Scripts\activate

Enter fullscreen mode Exit fullscreen mode

On Mac/Linux:

source venv/bin/activate

Enter fullscreen mode Exit fullscreen mode

Your terminal prompt will change to show (venv) at the start. That means the virtual environment is active. Every time you come back to work on this project, you’ll need to activate it again before running anything.

Step 5: Install the Dependencies

Our project uses a few external libraries — code that other people wrote so we don’t have to. They’re listed in a file called requirements.txt (also in the repo).

With your virtual environment active, run:

pip install -r requirements.txt

Enter fullscreen mode Exit fullscreen mode

You’ll see a stream of output as pip downloads and installs everything. This takes a minute or two depending on your internet speed. When it’s done, the prompt comes back and you’re ready.

The key packages being installed are:

  • google-api-python-client — lets us talk to the YouTube API
  • requests — lets us make HTTP calls to Spotify’s API
  • python-dotenv — reads our .env file and makes the values available to the script

Step 6: Run the Script

This is the moment. Make sure your virtual environment is still active (you’ll see (venv) in the prompt), then run:

python script.py

Enter fullscreen mode Exit fullscreen mode

The script starts running. The first few lines you’ll see are it connecting to Spotify and fetching the YouTube playlist:

Fetching YouTube playlist...
Found 150 videos on YouTube.

Enter fullscreen mode Exit fullscreen mode

Then it starts searching, one song at a time:

Searching: Stayin Alive Bee Gees
1. Found: Stayin' Alive by Bee Gees

Searching: Dancing Queen ABBA
2. Found: Dancing Queen by ABBA

Searching: Hotel California Eagles
3. Found: Hotel California - 2013 Remaster by Eagles

Searching: Bohemian Rhapsody Queen Official Music
4. Found: Bohemian Rhapsody by Queen

Searching: Some Obscure B-Side Title
5. Not found: Some Obscure B-Side Title - Artist Name

Enter fullscreen mode Exit fullscreen mode

It will churn through all 150. Most will be found. A few won’t — usually because the song isn’t on Spotify, or the YouTube title is too unusual for the search to crack. That’s normal.

When it’s done, you’ll see the summary:

Adding 143 new tracks...
Finished adding all 143 tracks.

Reordering playlist to match YouTube order...
Finished reordering 143 tracks.

--- Done ---
YouTube videos:     150
Found on Spotify:   143
Not found:          7
Added:              143
Removed:            0

Enter fullscreen mode Exit fullscreen mode

Open Spotify. Your playlist is there, populated, and in the same order as the YouTube playlist.

That just happened automatically. No clicking. No copy-pasting. You wrote a script that did it for you.

If Something Goes Wrong

ModuleNotFoundError: No module named 'dotenv'
Your virtual environment isn’t active. Run the activate command from Step 4 and try again.

Missing required environment variables: CLIENT_ID
Your .env file is either missing, in the wrong folder, or has the wrong name (check for .env.txt on Windows). It must be in the same folder as script.py.

401 Unauthorized from Spotify
Your CLIENT_ID, CLIENT_SECRET, or REFRESH_TOKEN is wrong. Go back to Chapter 2 and double-check the values.

HttpError 403 from YouTube
Your API_KEY is wrong or the API isn’t enabled on your Google Cloud project. Go back to Chapter 3 and verify both.

HttpError 404 from YouTube
Your YOUTUBE_PLAYLIST_ID is wrong, or the playlist is set to private. The ID should match exactly what’s in the playlist URL.

The script runs but adds 0 tracks
Check that your SPOTIFY_PLAYLIST_ID is correct and that the Spotify app has playlist-modify-public and playlist-modify-private in its scopes (we set this in the authorization URL in Chapter 2).

What’s Next?

Running the script manually is great. Running it automatically every day without touching your computer is better.

In Chapter 6, we push the code to GitHub and set up a GitHub Action that runs the script on a schedule — every day, for free, forever.

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다