python - 获取 ModuleNotFoundError : No module named 'azure'

标签 python azure azure-devops azure-functions azure-pipelines

我正在使用 Azure Pipelines 并收到以下错误:

ImportError: Failed to import test module: QueryUrls
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.8.3/x64/lib/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "/home/vsts/work/1/s/QueryUrls/__init__.py", line 2, in <module>
    import azure.functions as func
ModuleNotFoundError: No module named 'azure'

当我为用 Python 编写的 Azure Function 运行单元测试时,会发生错误。我在管道中运行的代码(用于运行单元测试)如下:

- script: |
  python -m unittest QueryUrls/test_queryurls.py
displayName: 'Test with unittest'

在添加上述行之前,管道运行正常。这是正在调用的脚本:

# tests/test_httptrigger.py
import unittest
from pathlib import Path

import azure.functions as func
from . import main
#from QueryUrls import my_function

class TestFunction(unittest.TestCase):
    def test_my_function(self):
        # Construct a mock HTTP request.
        req = func.HttpRequest(
            method='GET',
            body=None,
            url='/api/QueryUrls',
            params={'name': 'World'})

        # Call the function.
        resp = my_function(req)

        # Check the output.
        self.assertEqual(
            resp.get_body(),
            b'Hello World',
        )

if __name__ == '__main__':
    unittest.main()

最佳答案

执行此操作的正确方法是拥有一个包含所有依赖项的需求 txt

 pip install -r requirements.txt -r requirements-test.txt

将azure-functions添加到requirements-text.txt并在开头运行脚本

关于python - 获取 ModuleNotFoundError : No module named 'azure' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62953789/

相关文章:

python csv 到字典

python - 仅具有高亮值的 matplotlib 条形图

python - 在 Hadoop Streaming 中创建一个单独的函数来读取文件的问题

Azure Front Door 后端详细介绍了现有 Front Door 问题的补充

azure - 如何查询 dev azure 项目中所有管道的 yaml 文件路径?

python - 如何通过 Python SDK 设置 azure 容器应用程序的身份验证选项?

azure - Kafka 在 Azure Kubernetes 集群中工作,但不在本地 minikube 中工作

azure - Windows Azure 上的网站、云服务和虚拟机应如何选择

ios - Xamarin.ios Azure Devops 构建失败

github - 从 Azure Pipeline 创建 GitHub PR 评论