[Python] Python Azure Function nested code can't find modules even with init files

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    I made a test.py, and it works fine.. So this has to be how Azure is starting the function.

    import sys
    from pathlib import Path

    # Get the directory of the current file (test_function.py)
    current_dir = Path(__file__).parent

    # Add the 'code' directory to the PYTHONPATH
    sys.path.append(str(current_dir / 'code'))

    try:
    from code.db.database import SessionLocal
    print("Import successful: SessionLocal is available.")
    except ModuleNotFoundError as e:
    print(f"Import failed: {e}")



    ----------update above


    I reorganized my code from a flat hierarchy to nested. Azure functions has my Python version locked at 3.12.8 I added all the init.py files, tired 20 variations of sys path. I am not sure why this won't find the files.

    [​IMG]

    Found Python version 3.12.8 (py).


    MY function_app.py

    import os
    import azure.functions as func
    from pathlib import Path
    import logging
    import json
    from datetime import datetime
    import sys
    # Add parent directory to Python path
    import sys
    from pathlib import Path

    # Get the directory of the current file (function_app.py)
    current_dir = Path(__file__).parent # ✅ Correct syntax
    sys.path.append(os.path.join(os.path.dirname(__file__), 'code'))

    # Navigate to the project root (adjust based on your structure)
    project_root = current_dir.parent # Assumes KUSA is the parent of kusa.marketing
    sys.path.insert(0, str(project_root))

    # Now import your module
    from code.db.database import SessionLocal


    from code.db.database import SessionLocal # Changed to relative import
    from generate_jobber_auth import generate_jobber_auth_url, handle_jobber_callback
    from JobberSyncManager import JobberSyncManager
    from jobber_auth_models import JobberToken

    app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

    @app.route(route="buildcompanyprofile", auth_level=func.AuthLevel.ANONYMOUS)
    def buildcompanyprofile(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')


    Azure Functions Core Tools Core Tools Version: 4.0.6821 Commit hash: N/A +c09a2033faa7ecf51b3773308283af0ca9a99f83 (64-bit) Function Runtime Version: 4.1036.1.23224

    Exception: ModuleNotFoundError: No module named 'code.db'; 'code' is not a package. Cannot find module. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound. Current sys.path: ['C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64', 'C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64', 'C:\Users\Steve\AppData\Local\Programs\Python\Python312\python312.zip', 'C:\Users\Steve\AppData\Local\Programs\Python\Python312\DLLs', 'C:\Users\Steve\AppData\Local\Programs\Python\Python312\Lib', 'C:\Users\Steve\AppData\Local\Programs\Python\Python312', 'C:\Users\Steve\Documents\GitHub\KUSA\kusa.marketing\.venv', 'C:\Users\Steve\Documents\GitHub\KUSA\kusa.marketing\.venv\Lib\site-packages', 'C:\Users\Steve\Documents\GitHub\KUSA\kusa.marketing'] Stack: File "C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 469, in handle__functions_metadata_request self.load_function_metadata( File "C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 449, in load_function_metadata self.index_functions(function_path, function_app_directory))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64\azure_functions_worker\dispatcher.py", line 822, in index_functions indexed_functions = loader.index_function_app(function_path) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 49, in call raise extend_exception_message(e, message) File "C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64\azure_functions_worker\utils\wrappers.py", line 44, in call return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Steve\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.12\WINDOWS\X64\azure_functions_worker\loader.py", line 244, in index_function_app imported_module = importlib.import_module(module_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Steve\AppData\Local\Programs\Python\Python312\Lib\importlib_init
    .py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Steve\Documents\GitHub\KUSA\kusa.marketing\function_app.py", line 21, in from code.db.database import SessionLocal

    Continue reading...

Compartilhe esta Página