4

I have this local.settings.json in my local Azure function (Http triggered)

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  },
  "ConnectionStrings": {
    "SQLConnectionString": "valid connection string "
  }
}

I want to access variable "SQLConnectionString" and use in my Python3.6 code. I have found many guides for accessing this variable with C#, but I wasn't lucky to find how to do it via python.

2
  • Do you mind set the properties(such as username, password, port....) separately in the connect operation instead of connection string ? Commented Oct 24, 2019 at 6:36
  • Connection string will be hidden in Azure key Vault and accessed via azure env variables in future. But for testing purposes and developement I would like to have connection string in local.settings.json. Commented Oct 24, 2019 at 6:46

4 Answers 4

7

I test it just now and met the same error. You should modify your local.setting.json as below:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "SQLConnectionString": "cnx = mysql.connector.connect(user=\"test@hurytestmysqlserver\", password=\"Password123\", host=\"testmysqlserver.mysql.database.azure.com\", port=3306, database=\"xxx\", .....)"
  }
}

but not

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  },
  "ConnectionStrings": {
    "SQLConnectionString": "xxxxxxxx"
  }
}

Please have a try.

Just as a supplement~(I wanted to provide the solution "os.environ[xxx]" but saw the solution provided by HariHaran, haha~)

Sign up to request clarification or add additional context in comments.

2 Comments

yup modifying the local.setting.json without having nested "ConnectionStrings": { "SQLConnectionString": "xxxxxxxx" } helped. Thanks a lot!
Warning: in your sentence I think you missed the "s" in the word "settingS" to have really a filename named as "local.settings.json" instead of "local.setting.json". In my case once well named and with your right syntax all is running well.
7

You can access these settings by declaring import os and then using

setting = os.environ["setting-name"].

More information - Developer Reference

1 Comment

Tried: import os .... print(os.environ["SQLConnectionString"]) or print(os.environ["ConnectionStrings"]) With the same result: System.Private.CoreLib: Exception while executing function: Functions.HttpTrigger. System.Private.CoreLib: Result: Failure Exception: KeyError: 'SQLConnectionString'
7

In Python you need to do following:

import os
conn = os.environ["ConnectionStrings:SQLConnectionString"]

1 Comment

This is the best answer
2

Hi you can also try this:

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
  "AzureWebJobsStorage": "",
  "FUNCTIONS_WORKER_RUNTIME": "python",
  "SQLConnectionString": "cnx = mysql.connector.connect(user=\"test@hurytestmysqlserver\", password=\"Password123\", host=\"testmysqlserver.mysql.database.azure.com\", port=3306, database=\"xxx\", .....)"
  }
}

python:

import os
SQLConnectionString = os.getenv("SQLConnectionString")

Remember to configure the azure function environment aswell otherwise the function will fail on deployment.

Comments

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.