0

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.

enter image description here

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

7
  • You have an __init__.py file in the db folder, but it doesn't look like there is one in the code folder. Try creating one there. Commented Mar 4 at 16:57
  • it's there, below all the db code Commented Mar 4 at 17:04
  • Are you sure? __pycache__ is right at the top, immediately under code, so I would expect __init__.py to be at the top also . Commented Mar 4 at 17:08
  • I can't post a picture, but i hovered over it and the path is correct. I just ran a separate python test method, and it's working there. So it's something to do with how azure starts the function. -- updating the sample. Commented Mar 4 at 17:11
  • That's odd. __pycache__ and __init__.py are listed right next to each other in the db folder. Why wouldn't they also be listed next to each other in the code folder? Commented Mar 4 at 17:13

1 Answer 1

0

Had to add this to my settings.json and then get all the fully qualified imports correctly done. Like this: from code.jobber.jobber_api import JobberAPI NOTE:"Pylance quick fix add imports sucks and does not honor this. Ifg anyone knows how to fix VS code to import the full path by default. Please let me know

{
    "azureFunctions.deploySubpath": ".",
    "azureFunctions.scmDoBuildDuringDeployment": true,
    "azureFunctions.projectLanguage": "Python",
    "azureFunctions.projectRuntime": "~4",
    "debug.internalConsoleOptions": "neverOpen",
    "azureFunctions.projectLanguageModel": 2,
    "python.pythonPath": "/usr/bin/python",
    "python.autoComplete.extraPaths": [
      "${workspaceFolder}/Code/db",
      "${workspaceFolder}/Code/models",
      "${workspaceFolder}/Code/jobber",
      "${workspaceFolder}/Code",
      "${workspaceFolder}/Code/google",       
      "${workspaceFolder}/Code/tests",
      "${workspaceFolder}/Code/mailer_lite",
      "${workspaceFolder}/Code/apify",
      "$./Code/db",
      "$./Code/models",
      "$./Code/jobber",
      "$./Code",
      "$./Code/google",       
      "$./Code/tests",
      "$./Code/mailer_lite",
      "$./Code/apify"
    ],
    "python.analysis.extraPaths": [
        "${workspaceFolder}/Code/db",
        "${workspaceFolder}/Code/models",
        "${workspaceFolder}/Code/jobber",
        "${workspaceFolder}/Code",
        "${workspaceFolder}/Code/google",       
        "${workspaceFolder}/Code/tests",
        "${workspaceFolder}/Code/mailer_lite",
        "${workspaceFolder}/Code/apify",
        "$./Code/db",
        "$./Code/models",
        "$./Code/jobber",
        "$./Code",
        "$./Code/google",       
        "$./Code/tests",
        "$./Code/mailer_lite",
        "$./Code/apify"
    ],
Sign up to request clarification or add additional context in comments.

1 Comment

then finally to get the azure func to work . import os import sys import azure.functions as func from pathlib import Path import logging import json dir_path = os.path.dirname(os.path.realpath(file)) sys.path.insert(0, dir_path) from datetime import datetime from database import SessionLocal from code.jobber.generate_jobber_auth import generate_jobber_auth_url, handle_jobber_callback from code.jobber.JobberSyncManager import JobberSyncManager from code.models.jobber_models import JobberToken

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.