在使用 docker-compose up 启动时,我的 Flask 容器中出现此错误:
web_1 | Traceback (most recent call last):
web_1 | File "/app/app.py", line 3, in <module>
web_1 | from presentation.controllers.notifications import notifications_router
web_1 | File "/app/presentation/controllers/notifications.py", line 4, in <module>
web_1 | from app.presentation.models.notifications_body import NotificationsBody
web_1 | File "/app/app.py", line 3, in <module>
web_1 | from presentation.controllers.notifications import notifications_router
web_1 | ImportError: cannot import name 'notifications_router' from partially initialized module 'presentation.controllers.notifications' (most likely due to a circular import) (/app/presentation/controllers/notifications.py)
如果我在 docker-compose 之外运行它,它可以正常工作,但是在 docker-compose 中它会出现错误,有人知道如何修复它吗?
这是我的 docker-compose.yml
version: '3.9'
services:
web:
build:
context: .
dockerfile: Dockerfile
ports:
- "5000:5000"
depends_on:
- mongodb
- rabbitmq
- nginx
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
mongodb:
image: mongo:latest
ports:
- "27017:27017"
environment:
- MONGO_INITDB_DATABASE=test
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
- MONGO_URI="mongodb://admin:admin@mongodb:27017/"
volumes:
- mongodb_data:/data/db
mongo-express:
image: mongo-express:latest
ports:
- "8081:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- ME_CONFIG_MONGODB_ADMINPASSWORD=admin
- ME_CONFIG_MONGODB_SERVER=mongodb
- ME_CONFIG_MONGODB_ENABLE_ADMIN=true
- ME_CONFIG_BASICAUTH_USERNAME=admin
- ME_CONFIG_BASICAUTH_PASSWORD=admin
depends_on:
- mongodb
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
restart: always
environment:
- RABBITMQ_DEFAULT_USER=admin
- RABBITMQ_DEFAULT_PASS=admin
- RABBITMQ_DEFAULT_VHOST=/
ports:
- 15672:15672
volumes:
- rabbitmq_data:/var/lib/rabbitmq
volumes:
mongodb_data:
rabbitmq_data:
应用程序.py
from app.infrastructure.server import entrypoint
app = entrypoint.start()
if __name__ == '__main__':
app.run(host='0.0.0.0')
入口点.py
from flask import Flask
from app.infrastructure.server import url_mapping
def start():
"""
Starts the application
Returns:
Flask: Flask application
"""
app = Flask(__name__)
app.url_map.strict_slashes = False
url_mapping.map_routes(app)
app.app_context().push()
return app
url_mapping.py
from flask import Flask
from app.presentation.controllers.notifications import notifications_router
from app.presentation.controllers.ping_ctr import ping_router
def map_routes(application: Flask) -> None:
"""
Map routes
Args:
application (Flask): Flask application
"""
application.register_blueprint(ping_router)
application.register_blueprint(notifications_router)
有人可以帮忙吗?谢谢!
查看您的
app.presentation.models.notifications_body
文件
from app.presentation.models.notifications_body import NotificationsBody
看起来像是从 app.py 导入了一些东西
File "/app/app.py"
如果没有帮助,请将 Dockerfile 添加到您的问题的描述中