首页 > 解决方案 > Flask-ImportError: ModuleNotFoundError: No module named 'mycode'

问题描述

ImportError while loading conftest '/home/rohit/flask/src/tests/conftest.py'.
conftest.py:6: in <module>
    from mycode import create_app
E   ModuleNotFoundError: No module named 'mycode'

当从 mycode 文件夹上方的 src 文件夹运行 pytest 时,它会给出 ModuleNotFoundError

目录结构如下

├── mycode
│   ├── auth
│   ├── auth.py
│   ├── db_insert.py
│   ├── db.py
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── auth.cpython-37.pyc
│   │   ├── db.cpython-37.pyc
│   │   ├── db_insert.cpython-37.pyc
│   │   └── __init__.cpython-37.pyc
│   ├── README.md
│   ├── schema.sql
│   └── templates
│       ├── auth
│       │   ├── login.html
│       │   └── register.html
│       └── base.html
└── tests
    ├── conftest.py
    ├── data.sql
    ├── __pycache__
    │   └── conftest.cpython-37-pytest-5.4.1.pyc
    └── test_factory.py

标签: pythonflask

解决方案


您的__init__.py文件需要放在名为mycode. 如果上述情况已经成立,请尝试以下操作。

重命名__init__.pymycode.py

您可以在此处查看更多可能解决您问题的解决方案。


推荐阅读