简体   繁体   English

uwisg:ModuleNotFoundError:没有名为“flask”的模块

[英]uwisg: ModuleNotFoundError: No module named 'flask'

I am trying to make a flask application, and I am following the well known tutorials out there.我正在尝试制作一个 flask 应用程序,并且我正在关注那里众所周知的教程。 I am using docker containers, so I don't need virtual environments, as the containers are already isolated environments.我正在使用 docker 容器,所以我不需要虚拟环境,因为容器已经是隔离环境。

that's my project folder for the flask app:这是我的 flask 应用程序的项目文件夹:

flask
├── Dockerfile
├── app
│   ├── __init__.py
│   ├── routes.py
│   ├── static
│   └── templates
│       ├── index.html
│       └── test.html
├── requirements.txt
├── run.py
└── uwsgi.ini

When starting up the container with docker-compose, I get the following error.使用 docker-compose 启动容器时,出现以下错误。

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from app import app #as application
  File "./app/__init__.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'
unable to load app 0 (mountpoint='') (callable not found or import error)

I have read many other questions with such an error, but none of those solutions helped or had to do with venv (which I am not using).我已经阅读了许多其他带有此类错误的问题,但是这些解决方案都没有帮助或与 venv (我没有使用)有关。 So I hope someone can pinpoint, where the problem might be.所以我希望有人能查明问题可能出在哪里。

I have installed flask in Dockerfile with我已经在 Dockerfile 中安装了 flask

RUN python3 -m pip install  --disable-pip-version-check --no-cache-dir -r /tmp/requirements.txt

(I also just used pip3 install... but it does not make a difference, flask is installed either way). (我也只是使用pip3 install...但它没有任何区别,flask 以任何一种方式安装)。 from the build log:从构建日志:

Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 flask-2.0.2 itsdangerous-2.0.1
## run.py
from app import app

This is the import that seems to fail:这是似乎失败的导入:

## __init__.py
from flask import Flask
app = Flask(__name__)
from app import routes

extract from uwisg.ini:从 uwisg.ini 中提取:

[uwsgi]
plugin = python3
wsgi-file = run.py
callable = app

After starting the container with docker-compose, I exec into the container, start a Python3 interactive shell and have no problems at all doing from flask import Flask . After starting the container with docker-compose, I exec into the container, start a Python3 interactive shell and have no problems at all doing from flask import Flask .

I have tried running the container as root but that did not change anything.我尝试以 root 身份运行容器,但这并没有改变任何东西。

Any ideas?有任何想法吗? thanks!谢谢!

Ok, after more rinse and repeat trying, I figured it out.好的,经过更多冲洗并重复尝试,我想通了。

In my Dockerfile I also set the PYTHONPATH to my workspace:在我的 Dockerfile 中,我还将PYTHONPATH设置为我的工作区:

ENV PYTHONPATH "${PYTHONPATH}:/workspace/flask"
ENV PYTHONPATH "${PYTHONPATH}:/workspace/flask/app"

and site-packages apperently could not be accessed by Python anymore. Python 显然无法访问site-packages

I also had to add this:我还必须添加这个:

ENV PYTHONPATH "${PYTHONPATH}:/usr/local/lib/python3.11/site-packages"

I have no idea, why this path was not accessible for Python?我不知道,为什么 Python 无法访问此路径? Did I overwrite sys.path when using ENV PYTHONPATH ?使用ENV PYTHONPATH时我是否覆盖了sys.path Should this not be additive to sys.path?这不应该添加到 sys.path 吗?

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

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