question

venkateswaranP-1646 avatar image
0 Votes"
venkateswaranP-1646 asked anthonychu-msft answered

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anthonychu-msft avatar image
3 Votes"
anthonychu-msft answered

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:

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

venkateswaranP-1646 avatar image
0 Votes"
venkateswaranP-1646 answered MayankBargali-MSFT converted comment to answer

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@venkateswaranP-1646 Thanks for sharing your resolution. Feel free to 'Accept as answer' so that it can help others in the community looking for help on similar topics.

0 Votes 0 ·

sorry the location only when I run locally when I deploy it shows location not found


 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:


0 Votes 0 ·