【问题标题】:how to use Streamlit and Pytest in the same project如何在同一个项目中使用 Streamlit 和 Pytest
【发布时间】:2022-01-26 07:47:27
【问题描述】:

当我尝试在同一个项目中使用 Pytest 和 Streamlit 时,模块导入出现问题,我不知道如何处理。

我的目录如下:

repo
├── main.py
├── app.py
├── utils
│   ├── __init__.py
│   ├── util_a.py
└── tests
     ├── __init__.py
     └── test_a.py

app.py 文件包含下一个导入行:

import utils.util_a 
...

现在,streamlit 应用程序按预期运行,代码如下: streamlit run repo/app.py

但是当我运行py.test 时,我收到一条错误消息:

ModuleNotFoundError: No module named 'utils'

如果我将app.py 中的导入行替换如下:

import repo.utils.util_a 
...

令人惊讶的是,Pytest 按预期运行,但 Streamlit 应用程序返回下一个错误:

ModuleNotFoundError: No module named 'repo'

该错误的根本原因是什么,我该如何处理?如何在同一个项目中使用 Pytest 和 Streamlit 而不会导致导入冲突?

谢谢!

【问题讨论】:

标签: python pytest python-import streamlit


【解决方案1】:

repo 中添加conftest.py 文件即可解决问题。现在,目录如下所示:

repo
├── main.py
├── app.py
├── conftest.py
├── utils
│   ├── __init__.py
│   ├── util_a.py
└── tests
     ├── __init__.py
     └── test_a.py

更多详情that post

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2022-11-21
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    相关资源
    最近更新 更多