Azure function not able to read my json file from my python

venkateswaran P 116 Reputation points
2021-08-17T04:29:34.513+00:00

I am trying to run my python code in the azure function.

I want to run my code 24/7.

I have three files in my app folder

init.py

from .test import *

still getting an error in azure deploy

Exception while executing function: Functions.HttpTrigger1 <--- Result: Failure Exception: FileNotFoundError: [Errno 2] No such file or directory: 'HttpTrigger1\\setting_test.json' Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 398, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.9/concurrent/futures/thread.py", line 52, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 602, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/extension.py", line 215, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/HttpTrigger1/__init__.py", line 21, in main test.run() File "/home/site/wwwroot/HttpTrigger1/test.py", line 191, in run config = read_setting() File "/home/site/wwwroot/HttpTrigger1/test.py", line 188, in read_setting with open('HttpTrigger1\setting_test.json') as json_file:

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        test.run()

I calling my test.py file

there I have a file called setting_test.json to get data to run in the code.

def read_setting():
        with open('setting_test.json') as json_file:
            return json.load(json_file)

but the files were running in just like a python file but now it is not reading the given file

Python HTTP trigger function processed a request.
[2021-08-17T04:19:19.233Z] Executed 'Functions.HttpTrigger1' (Failed, Id=2ea68df6-4fd6-42be-a1b6-1016a02d49fa, Duration=72ms)
[2021-08-17T04:19:19.234Z] System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger1. System.Private.CoreLib: Result: Failure
Exception: FileNotFoundError: [Errno 2] No such file or directory: 'setting_test.json'
Stack:   File "C:\Users\HG\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 398, in _handle__invocation_request
    call_result = await self._loop.run_in_executor(
  File "C:\Python39\lib\concurrent\futures\thread.py", line 52, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\HG\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 602, in _run_sync_func
    return ExtensionManager.get_sync_invocation_wrapper(context,
  File "C:\Users\HG\AppData\Roaming\npm\node_modules\azure-functions-core-tools\bin\workers\python\3.9/WINDOWS/X64\azure_functions_worker\extension.py", line 215, in _raw_invocation_wrapper
    result = function(**args)
  File "F:\tradetest\bitfinexsupertrend\HttpTrigger1\__init__.py", line 21, in main
    test.run()
  File "F:\tradetest\bitfinexsupertrend\HttpTrigger1\test.py", line 192, in run
    config = read_setting()
  File "F:\tradetest\bitfinexsupertrend\HttpTrigger1\test.py", line 188, in read_setting
    with open('setting_test.json') as json_file:

how do i get the data to the python test.py file to runn the code.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,313 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anthony Chu - MSFT 856 Reputation points Microsoft Employee
    2021-08-17T05:53:31.717+00:00

    The reason for the original issue is because open() expects a path relative to the working directory or an absolute path. When you run test.py directly, the working directory is the location of test.py. When you run this in Azure Functions, the working directory is the root of the function app. This is why open('HttpTrigger1\setting_test.json') works locally.

    The reason it doesn't work in Azure is because in Azure, the app runs on Linux and its path separator is /, not \. Use os.path.join() to build your path in a platform agnostic manner:

    import os
    
    # ...
    with open(os.path.join('HttpTrigger1', 'setting_test.json')) as json_file:
    

    More generically, you can use os.path.dirname(__file__) to obtain the folder where your script resides instead of hardcoding it to the folder name:

    import os
    
    # ...
    script_dir = os.path.dirname(__file__)
    with open(os.path.join(script_dir, 'setting_test.json')) as json_file:
    
    4 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. venkateswaran P 116 Reputation points
    2021-08-17T04:35:11.003+00:00

    got it

     with open('HttpTrigger1\setting_test.json') as json_file:
    

    it was just a location error but the location is not found in the azure function when deployed