首页 > 解决方案 > 在 Azure 函数中使用类和外部 py 文件

问题描述

我正在使用 HTTP 触发器模板处理 Azure Functions。

我正在尝试调用另一个文件中的类,但是当我运行代码时,控制台会显示一个错误:

在此处输入图像描述

这是我拥有的代码:

在此处输入图像描述

在此处输入图像描述

我在文档中搜索,但我没有发现任何关于此的内容。我可以做些什么来处理我的文件和他们的类?

谢谢 :)

标签: python-3.xazure-functionsazure-function-appazure-functions-core-tools

解决方案


If you want to import custom class in Azure Function, we should use explicit relative or absolute references. At the moment, Azure function doesn't support implicit relative imports of the form import xxx from within the project. For more details, please refer to the document

For example

__app__ is the function application's top level module and can be used as an anchor for performing absolute imports.

__app__
  Function1
    __init__.py
    func_module.py
  HelperCode
    __init__.py
    helper_module.py

we can use the following sytaxes (as examples):

  1. from __app__ import HelperCode
  2. from __app__.HelperCode import helper_module
  3. import __app__.Function1
  4. import __app__.HelperCode

推荐阅读