pythonflaskimporterrormodulenotfounderrorspotipy

No module named 'client' when running Flask web app with Spotipy


I'm encountering an ImportError when trying to run a Flask web app for a music recommender project. The error message I receive is:

Traceback (most recent call last):
  File "/data/data/com.termux/files/home/music_recommender/app.py", line 4, in <module>
    app = create_app()
  File "/data/data/com.termux/files/home/music_recommender/app/__init__.py", line 18, in create_app
    from app import routes
  File "/data/data/com.termux/files/home/music_recommender/app/routes.py", line 2, in <module>
    import spotipy
  File "/data/data/com.termux/files/home/music_recommender/venv/lib/python3.11/site-packages/spotipy/__init__.py", line 2, in <module>
    from client import Spotify, SpotifyException
ModuleNotFoundError: No module named 'client'

I believe the issue is related to the spotipy module and its 'client' submodule. However, I'm unsure why it can't find this submodule.

I have cloned the repository from this GitHub link: GitHub Repository.

I have tried to run the Flask web app by executing python app.py in the project's root directory.

Instead of the Flask website running successfully, I encountered the ImportError mentioned above.

I have reviewed the code and made sure that I've installed all the necessary dependencies mentioned in the project's requirements.txt file.


Solution

  • I've cloned the repository and was able to run the project after executing the following steps:

    Update spotipy version

    The version of spotipy that exists inside the project's requirements.txt file is outdated. Updating to its latest version fixes the ModuleNotFoundError exception you're experiencing. To update the package, run the following command:

    $ pip install --upgrade spotipy
    

    Fix NameError Exception

    Even after fixing problem regarding the spotipy version, if you try to execute the command python app.py, you'll still receive an NameError. It seems that the module app/routes.py is trying to reference the object named app, that is created by the function create_app() from app/__init__.py but is unable to find it. To fix this, you can perform the following modifications on the project modules:

    app/__init__.py

    Replace the source code from app/__init__.py with:

    import os
    
    import spotipy
    import spotipy.util as sp_util  # Import Spotify utility functions
    from config import Config
    from flask import (Flask, redirect, redirect, render_template, render_template, request,
                       request, session, session, url_for, url_for)
    from flask_session import Session
    from spotipy.oauth2 import SpotifyOAuth, SpotifyOAuth
    
    
    # Initialize and configure Flask-Session
    session = Session()
    
    
    def create_app():
        app = Flask(__name__)
        app.config.from_object(Config)
    
        # Configure Flask-Session
        app.config['SESSION_TYPE'] = 'filesystem'
        app.config['SESSION_PERMANENT'] = False
        session.init_app(app)
        return app
    
    
    app = create_app()
    from app.routes import *
    

    app/routes.py

    Add the import from app import app to the module app/routes.py:

    from flask import render_template, redirect, url_for, session, request
    import spotipy
    from spotipy.oauth2 import SpotifyOAuth
    import spotipy.util as sp_util  # Import Spotify utility functions
    import os
    
    from app import app  # <-- Add this import HERE
    
    # Define Spotify API credentials and settings
    client_id = os.environ.get('SPOTIPY_CLIENT_ID')
    # ...
    

    app.py

    Instead of the import from app import create_app, replace it with from app import app. Here's the app.py script, with the modified import statement:

    from flask import Flask
    from app import app
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    After the previously mentioned modifications, you should be able to run the command python app.py and access the web app:

    enter image description here