1

I'm just starting with Azure Functions using Python. My library import of dnspython is not being recognized properly in Visual Studio Code.

I have Visual Studio Code v1.74 and am writing an Azure Function in Python. I did a PIP IMPORT of the Python library DNSPYTHON and verified that it is in my local environment via pip -list in the Terminal window of Visual Studio Code. However, the import Intellisense throws an error saying "Import "dnspython" could not be resolved." Screenshot below

Import DNSPYTHON throws pylance error, but PIP LIST shows it in the local environment

enter image description here Any suggestions of what I could try to resolve this? I'm wondering if there is some sort of PATH problem perhaps.

EDIT: I'm using v2 of the programming model

zeeNope
  • 11
  • 4

1 Answers1

0

Import "dnspython" could not be resolved Pylance (reportMissingImports)

I have created the Python v2 model Azure Functions - Http Trigger using VS Code and written the sample code for using the package dns.

import  azure.functions  as  func
import  logging
import  dns
import  dns.resolver

app = func.FunctionApp()
@app.function_name(name="HttpTrigger1")
@app.route(route="hello")
def  test_function(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

#example code for import dns
result = dns.resolver.query('google.com', 'A')
for  ipval  in  result:
    print('IP', ipval.to_text())

#example code for import dns.resolver
answers = dns.resolver.resolve('dnspython.org', 'MX')
for  rdata  in  answers:
    print('Host', rdata.exchange, 'has preference', rdata.preference)

return  func.HttpResponse(f"Hello Krishna, This HTTP triggered function executed successfully.", status_code=200)

enter image description here

The packages dns and dns.resolver are working successfully in Python Azure Functions v2 model. To solve the import error issue, follow the below steps:

  1. Activate the Virtual Environment in the Project Terminal of VS Code.
  2. Then install the required packages using pip command.
  3. In requirements.txt file, mention the packages used in the function code file such as:
azure-functions
dns
dns.resovler

Note: Above parts of the code taken from references dnspython examples site and Charalamm's Issue.