You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function导入Numpy报ImportError,已通过Pip安装仍异常

Azure Function中Numpy导入错误的解决方法

运行Azure Function时触发ImportError,提示不能从Numpy源码目录导入该库,需退出源码目录重启Python解释器,但已通过Pip安装Numpy后问题依旧。相关信息如下:

错误日志

Worker failed to function id 2c284181-1e21-4200-b5ea-b7d226696206.
[2023-11-30T03:24:02.694Z] Result: Failure
[2023-11-30T03:24:02.694Z] Exception: ImportError: Error importing numpy: you should not try to import numpy from
[2023-11-30T03:24:02.694Z]         its source directory; please exit the numpy source tree, and relaunch
[2023-11-30T03:24:02.694Z]         your python interpreter from there. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
[2023-11-30T03:24:02.695Z] Stack:   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/dispatcher.py", line 314, in _handle__function_load_request
[2023-11-30T03:24:02.695Z]     func = loader.load_function(
[2023-11-30T03:24:02.695Z]   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call
[2023-11-30T03:24:02.695Z]     raise extend_exception_message(e, message)
[2023-11-30T03:24:02.695Z]   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call
[2023-11-30T03:24:02.695Z]     return func(*args, **kwargs)
[2023-11-30T03:24:02.695Z]   File "/usr/local/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.9/OSX/X64/azure_functions_worker/loader.py", line 85, in load_function
[2023-11-30T03:24:02.695Z]     mod = importlib.import_module(fullmodname)
[2023-11-30T03:24:02.695Z]   File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
[2023-11-30T03:24:02.695Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2023-11-30T03:24:02.695Z]   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
[2023-11-30T03:24:02.695Z]   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
[2023-11-30T03:24:02.695Z]   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
[2023-11-30T03:24:02.696Z]   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
[2023-11-30T03:24:02.696Z]   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
[2023-11-30T03:24:02.696Z]   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
[2023-11-30T03:24:02.696Z]   File "/Users/zero/Documents/Github/project/fc/__init__.py", line 7, in <module>
[2023-11-30T03:24:02.696Z]     import numpy as np
[2023-11-30T03:24:02.696Z]   File "/Users/zero/Library/Python/3.9/lib/python/site-packages/numpy/__init__.py", line 135, in <module>
[2023-11-30T03:24:02.696Z]     raise ImportError(msg) from e

函数代码(init.py)

import os
import re
import logging
from pathlib import Path
import numpy as np

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('A function processed a request.')

    return func.HttpResponse(
        "success",
        status_code=200,
    )

依赖配置(requirements.txt)

numpy

解决步骤

  • 检查项目目录是否存在冲突文件:确认你的Azure Function项目目录(/Users/zero/Documents/Github/project/fc/)下没有numpy文件夹或numpy.py文件,这类文件会让Python优先导入本地文件而非已安装的库。如果存在,直接删除或重命名。
  • 验证环境与依赖路径匹配:运行which python3.9确认当前使用的Python解释器路径,再运行pip3.9 show numpy查看Numpy的安装位置,确保两者对应。若不匹配,切换到正确环境重新安装:
    python3.9 -m pip install numpy --upgrade --force-reinstall
    
  • 清理缓存并重启函数:停止本地运行的Azure Function,删除项目目录下的.venv(若使用虚拟环境)和__pycache__文件夹,再重新启动函数:
    rm -rf .venv __pycache__
    func start
    
  • 确保虚拟环境正确使用:如果使用虚拟环境,先激活再安装依赖和运行函数:
    python3.9 -m venv .venv
    source .venv/bin/activate  # macOS/Linux环境
    pip install -r requirements.txt
    func start
    

内容的提问来源于stack exchange,提问作者Nurdin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 06:00:04