简体   繁体   English

获取错误:ModuleNotFoundError:没有名为“api”的模块

[英]GET error : ModuleNotFoundError: No module named 'api'

I've got an error when I try to run pytest command.当我尝试运行 pytest 命令时出现错误。 The error is just when I running the application on docker, when I did it locally, it works.错误只是当我在 docker 上运行应用程序时,当我在本地执行时,它可以工作。 There's another curiosity about it, the swagger and de requests are working fine, just the test file doesn't.还有另一个好奇,swagger 和 de 请求工作正常,只是测试文件没有。 I have already tried:我已经尝试过:

python -m pytest tests/

pytest tests/test_api.py

docker-compose exec api pytest

I`ve got this error:我有这个错误:

ImportError while importing test module 'C:\Users\mathe\Desktop\Teste\bluestorm-api\api\tests\test_api.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
..\..\..\..\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests\test_api.py:5: in <module>
    import api.database.create_engine
E   ModuleNotFoundError: No module named 'api'

My files structure:我的文件结构:

在此处输入图像描述

My test.py file:我的 test.py 文件:

from sys import api_version
from fastapi.testclient import TestClient
from sqlalchemy import text, orm
from sqlmodel import MetaData, Table
from api.main import app
from api.database import engine


client = TestClient(app) 

class TestApiGet:

    def test_login(self):
        
        
        response = app.post(
            "/login",
            data = {
            "USERNAME": "",
            "PASSWORD": ""
        })
        assert response.status_code == 200

My docker-compose file:我的 docker-compose 文件:

version: "3.8"
services:
  api:
    build: .
    command: ["uvicorn", "api.main:app", "--host=0.0.0.0", "--port=8000"]
    volumes:
      - ./api:/code/api
    ports:
      - "8000:8000"

My docker file:我的 docker 文件:

FROM python:3.9-slim-buster

WORKDIR /code

# TODO: Multstage build, so the container does not runs with a compiler
RUN apt-get update && apt-get install curl build-essential unixodbc-dev wait-for-it -y

COPY ./requirements.txt .

RUN pip install -r requirements.txt

RUN apt-get remove build-essential -y

COPY ./api /code/api/

EXPOSE 8000

Your code is for the most part just fine you just have a path problem and a invalid test class.您的代码大部分都很好,您只是遇到了路径问题和无效的测试 class。

Also it is not good practice to have your test package into your api package.此外,将您的测试 package 放入您的 api package 也不是一个好习惯。

you should do something like this:你应该这样做:

.
├── api
│   ├── __init__.py
│   ├── main.py
│   └── routers
│   │   ├── __init__.py
│   │   ├── something.py
│   │...
├── tests
│   ├── __init__.py
│   └── test_something.py
├── requirements.txt
├── Dockerfile

Also you made a syntax error with you api/__init__.py , you did put 3x _ at the biginning另外,您在api/__init__.py上犯了语法错误,您确实在 biginning 处放了 3x _

your test.py file doesn't respect the norm, please check fastapi official test doc :您的 test.py 文件不符合规范,请查看fastapi 官方测试文档

from fastapi.testclient import TestClient
from api.main import app

client = TestClient(app)


def test_login():
    response = client.post(
        "/login",
        data={
            "USERNAME": "",
            "PASSWORD": ""
        }
    )
    assert response.status_code == 200

in the parent folder you call:在您调用的父文件夹中:

pytest

with above structure and a simple fastapi api in main.py:具有上述结构和 main.py 中的简单 fastapi api:

FAILED tests/test_something.py::TestApiGet::test_login - assert 404 == 200

When you have a basic test structure that is running fine, you can uses test classes but here is a simpler/easier solution for the beginning.当你有一个运行良好的基本测试结构时,你可以使用测试类,但这里有一个更简单/更容易的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM