Azure Functions 확장을 사용하는 VS Code를 사용하여 Azure Functions를 처음 사용하고 로컬에서 작은 예제를 작성하려고했습니다.
예:
# 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
)
numpy가 전역 적으로 설치되면이 코드는 잘 작동합니다. 그러나 가상 환경에만 설치하면 다음 오류가 발생합니다.
*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*
가상 환경에 numpy가 설치되어 있는지 여러 번 확인했으며 환경도 .vscode / settings.json 파일에 지정되어 있습니다.
virtualenv "worker_venv"의 pip 동결 :
$ 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 파일 :
{
"azureFunctions.deploySubpath": ".",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": "worker_venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~2",
"debug.internalConsoleOptions": "neverOpen"
}
문서에서 무언가를 찾으려고했지만 가상 환경과 관련하여 구체적인 내용을 찾지 못했습니다. 내가 뭔가 빠졌는지 모르겠어요?
편집 : 나는 Windows 10 컴퓨터 btw에 있습니다.
편집 : 아래 이미지에 내 프로젝트의 폴더 구조를 포함했습니다.
편집 : 아래 이미지에 가상 환경 Lib 폴더의 내용을 추가했습니다.
편집 : pip install numpy
아래 명령을 사용하여 터미널의 스크린 샷 추가
편집 : 새 가상 환경으로 새 프로젝트를 만들고 numpy를 다시 설치했습니다. 아래 스크린 샷은 여전히 문제가 지속됩니다.
편집 : 아래 launch.json 코드 추가
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to Python Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
해결
그래서 문제는 파이썬이나 VS 코드가 아닙니다. 문제는 내 컴퓨터 (새 노트북)의 실행 정책이 제한으로 설정되어 .venv\Scripts\Activate.ps1
스크립트를 실행할 수 없다는 것입니다.
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
.
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.
-----Update------
Run the command to to set execution policy and then activate the virtual environment:
set-executionpolicy remotesigned
.venv\Scripts\Activate.ps1
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다