4

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.

Example:

# 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:
        try:
            body = req.get_json()
        except ValueError:
            pass
        else:
            seed = body.get('seed')

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

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
azure-functions==1.3.0
flake8==3.8.3
importlib-metadata==1.7.0
mccabe==0.6.1
numpy==1.19.0
pycodestyle==2.6.0
pyflakes==2.2.0
zipp==3.1.0

.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"
    }
  ]
}

SOLVED

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

BlackCat
  • 160
  • 2
  • 8
  • Do you have numpy included in requirements.txt? https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#package-management – Anatoli Beliaev Jul 15 '20 at 20:45
  • Yeah I included it in requirements.txt – BlackCat Jul 16 '20 at 06:12
  • 1
    Everything looks good to me. I have run into similar mysterious problems in the past and resolved them by re-creating everything in a new directory. Kind of like the 'ol "restart your computer" trick that fixes mysterious computer problems. Maybe give that a try? – Tyson Jul 16 '20 at 06:44
  • @Tyson Tried as you and suggested, still doesn't work – BlackCat Jul 16 '20 at 06:52
  • Darn :(. I don't think this will fix your problem, but something else you could try is replacing "import numpy as np" with "from . import numpy as np" – Tyson Jul 16 '20 at 07:10

2 Answers2

5

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

-----Update------

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

set-executionpolicy remotesigned
.venv\Scripts\Activate.ps1
Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Thanks for your detailed answer. I run into this problem locally on my windows 10 machine. I checked my folder structure and it looks the same to me as yours. I'll include the image in my previous post. – BlackCat Jul 16 '20 at 06:18
  • @CuriousFly And could you please share a screenshot of your Terminal window (with the pip install command) ? – Hury Shen Jul 16 '20 at 06:25
  • @CuriousFly Can you show the content of Lib folder under your virtual environment? does the module be there? – Cindy Pau Jul 16 '20 at 06:33
  • 1
    @CuriousFly Could you please create another new project to develop your function because the old one may have some problem after many modification. Try again in the new project, and share a screenshot of "Terminal" window with the pip install command. – Hury Shen Jul 16 '20 at 06:36
  • I added the content of the lib folder in the main post – BlackCat Jul 16 '20 at 06:36
  • @HuryShen I created a new project and reinstalled numpy in the new virtual environment, added the screenshots in the main post, problem still persists. – BlackCat Jul 16 '20 at 06:51
  • 1
    @CuriousFly Try run `func host start` directly under the virtual environment. Still get error? – Cindy Pau Jul 16 '20 at 06:56
  • @CuriousFly It seems everything is ok, may I know how did you run/start the function ? Could you please share a screenshot of the command to run the function ? – Hury Shen Jul 16 '20 at 07:01
  • @BowmanZhu Ok, so when I run in with `func host start` it works fine, no errors. Is there a problem with VS Code? – BlackCat Jul 16 '20 at 07:02
  • @CuriousFly So it should be the problem with the vs code, it doesn't run function host under the virtual environment. – Cindy Pau Jul 16 '20 at 07:05
  • @HuryShen I tried to debug it through VS Code by pressing F5 – BlackCat Jul 16 '20 at 07:05
  • 2
    @CuriousFly When you press F5, it will not use the modules installed in virtual environment. – Hury Shen Jul 16 '20 at 07:05
  • @CuriousFly Please give us the settings.json in .vscode. – Cindy Pau Jul 16 '20 at 07:05
  • @BowmanZhu the settings.json file can be seen in the main post, I also added the launch.json file content – BlackCat Jul 16 '20 at 07:08
  • @HuryShen So is there a possibility to debug the code and us the modules installed in the virtual environment? – BlackCat Jul 16 '20 at 07:09
  • @CuriousFly Could you please share a screenshot of Terminal window after press F5 ? When I press F5, it shows [this](https://i.stack.imgur.com/xTniV.png). So it can run with virtual environment when I debug it. – Hury Shen Jul 16 '20 at 07:24
0

I could solve my issue uninstalling python3 (see here for a guide https://stackoverflow.com/a/60318668/11986067).

After starting the app functions via F5 or func start, the following output was shown:

enter image description here

This version was incorrect. I have chosen python 3.7.0 when creating the project in the Azure extension. After deleting this python3 version, the correct version was shown and the Import issue was solved:

enter image description here

Ling
  • 449
  • 6
  • 21