Search code examples
pythonazureazure-functions

Azure Functions with VS Code and Python: ModuleNotFoundError: No module named 'azure.storage'


I am developing my first Function App with Python and VS Code, converting a local python script that worked to be scheduled. I have run across the following error that I am unable to solve:

Exception: ModuleNotFoundError: No module named 'azure.storage'. Please check the requirements.txt file for the missing module.

My original python script had local files that I referenced by filepath, but naturally for Azure Functions, I need to access blob storage so I made the appropriate changes to file download / upload and import references. I am only testing it locally at the moment with func start, and my function is http triggered.

Here is what I've tried so far:

1.) Changed my import references to:

from azure.storage.blob import BlobServiceClient

2.) Verified I'm working in my virtual environment in the terminal, .venv 3.) Add 'azure-storage-blob' to my requirements.txt file 3.) pip install --target .python_packages/lib/site-packages -r requirements.txt 4.) pip install -r requirements.txt 5.) Added additional values in my local.settings.json file:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "PYTHONPATH": ".python_packages/lib/site-packages",
    "python": ".venv/Scripts/python.exe"
  },
  "ConnectionStrings": {
    "MyDatabaseConnectionString": "my_connection_string"
  }
}

6.) Deleted and recreated the virtual environment and running steps 3 and 4 again. 7.) Verified that the selected python interpreter is 3.10.10 which matches what is in my virtual environment

The only other information I've come across is that azure.storage package was deprecated which is why I install azure-storage-blob. How can I resolve this error?


Solution

  • I have solved the error. My Python environment seemed to be flawed so I recreated the project. Firstly, I was using Python 3.10 when only up to version 3.9 is supported at the moment. I selected the Python 3.9.0 interpreter and adjusted the following settings in the project folder:

    1.) ProjectFolder/.vscode/settings.json - add the following line to the json:

    "python.defaultInterpreterPath": "ProjectFolderPath/.venv/Scripts/python.exe"

    2.) ProjectFolder/local.settings.json - add the following line to Values:

    "PYTHONPATH": "./ProjectFolderName"

    Afterwards, I deleted my virtual environment, recreated it, ran the pip install -r requirements.txt command in the terminal with the virtual environment activated, and now my function works.