[SOLVED] Numpy module not found when working with Azure Functions in VS Code and virtualenv


I’m new to working with azure functions and tried to work out a small example locally, using VS Code with the Azure Functions extension.


# First party libraries
import logging

# Third party libraries
import numpy as np
from azure.functions import HttpResponse, HttpRequest

def main(req: HttpRequest) -> HttpResponse:
    seed = req.params.get('seed')

    if not seed:
            body = req.get_json()
        except ValueError:
            seed = body.get('seed')

    if seed:
        r_int = np.random.randint(0, 100)
        return HttpResponse(
            "Random Number: " f"{str(r_int)}", status_code=200
        return HttpResponse(
            "Insert seed to generate a number",

When numpy is installed globally this code works fine. If I install it only in the virtual environment, however, I get the following error:

*Worker failed to function id 1739ddcd-d6ad-421d-9470-327681ca1e69.
[15-Jul-20 1:31:39 PM] Result: Failure
Exception: ModuleNotFoundError: No module named 'numpy'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound*

I checked multiple times that numpy is installed in the virtual environment, and the environment is also specified in the .vscode/settings.json file.

pip freeze of the virtualenv "worker_venv":

$ pip freeze

.vscode/settings.json file:

  "azureFunctions.deploySubpath": ".",
  "azureFunctions.scmDoBuildDuringDeployment": true,
  "azureFunctions.pythonVenv": "worker_venv",
  "azureFunctions.projectLanguage": "Python",
  "azureFunctions.projectRuntime": "~2",
  "debug.internalConsoleOptions": "neverOpen"

I tried to find something in the documentation, but found nothing specific regarding the virtual environment. I don’t know if I’m missing something?

EDIT: I’m on a Windows 10 machine btw

EDIT: I included the folder structure of my project in the image below

enter image description here

EDIT: Added the content of the virtual environment Lib folder in the image below

enter image description here

EDIT: Added a screenshot of the terminal using the pip install numpy command below

enter image description here

EDIT: Created a new project with a new virtual env and reinstalled numpy, screenshot below, problem still persists.

enter image description here

EDIT: Added the launch.json code below

  "version": "0.2.0",
  "configurations": [
      "name": "Attach to Python Functions",
      "type": "python",
      "request": "attach",
      "port": 9091,
      "preLaunchTask": "func: host start"


So the problem was neither with python, nor with VS Code. The problem was that the execution policy on my machine (new laptop) was set to restricted and therefore the .venv\Scripts\Activate.ps1 script could not be run.

To resolve this problem, just open powershell with admin rights and and run set-executionpolicy remotesigned. Restart VS Code and all should work fine

I didn’t saw the error, due to the many logging in the terminal that happens
when you start azure. I’ll mark the answer of @HuryShen as correct, because the comments got me to the solution. Thank all of you guys


For this problem, I’m not clear if you met the error when run it locally or on azure cloud. So provide both suggestions for these two situation.

1. If the error shows when you run the function on azure, you may not have installed the modules success. When deploy the function from local to azure, you need to add the module to requirements.txt(as Anatoli mentioned in comment). You can generate the requirements.txt automatically by the command below:

pip freeze > requirements.txt

After that, we can find the numpy==1.19.0 exist in requirements.txt.
enter image description here

Now, deploy the function from local to azure by the command below, it will install the modules success on azure and work fine on azure.

func azure functionapp publish <your function app name> --build remote

2. If the error shows when you run the function locally. Since you provided the modules installed in worker_venv, it seems you have installed numpy module success. I also test it in my side locally, install numpy and it works fine. So I think you can check if your virtual environment(worker_venv) exist in the correct location. Below is my function structure in local VS code, please check if your virtual environment locates in the same location with mine.

enter image description here


Run the command to to set execution policy and then activate the virtual environment:

set-executionpolicy remotesigned

Answered By – Hury Shen

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *