将Python脚本导入到测试中时出现“没有名为google.cloud的模块”错误,但直接运行时则没有

时间:2019-05-14 04:04:54

标签: python python-3.x pytest google-cloud-vision google-cloud-python

不知道这是否与google-cloud相关或与pytest相关。查看下面的文件。

当我运行python app/my_script.pypython -m app.my_script时,脚本运行良好。

但是当我运行pytest时,脚本from google.cloud import vision中的行将引发“ ModuleNotFoundError:没有名为'google.cloud'的模块”。

我尝试将各种软件包名称添加到requirements.txt文件中并/或运行带有pip install google-cloud标志和不带有pip install google-cloud-language标志的--upgradegoogle-cloud-vision 均未成功。我可以采取哪些步骤来克服此错误?


conftest.py :(空)

requirements.txt:

from google.cloud import vision
from google.cloud.vision import types

def new_client():
    client = vision.ImageAnnotatorClient()
    return client

if __name__ == "__main__":
    client = new_client()
    # etc...

app / my_script.py:

from app.my_script import new_client

# tests here... 

test / test_my_script.py:

largest = None
smallest = None

while True:
    num = input("Enter number:")
    if num == 'done':
        break
    try:
        num = int(num)
    except:
        print("Invalid input")
    if smallest is None or num < smallest:
        smallest = num
    if largest is None or largest > num:
        largest = num


print("Maximum", largest)
print("Minimum", smallest)

0 个答案:

没有答案