Python 在控制台中运行init_db()将不起作用

Python 在控制台中运行init_db()将不起作用,python,flask,Python,Flask,每当我尝试在python控制台中导入init db时: >>from app.database import init_db >>init_db() init db Traceback (most recent call last): File "<stdin>", line 1, in <module> File "app\database.py", line 18, in init_db import models Import

每当我尝试在python控制台中导入init db时:

>>from app.database import init_db
>>init_db()
init db
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "app\database.py", line 18, in init_db
    import models
ImportError: No module named models
为什么是这样

更新:这是我的项目结构

app/
   filebase/
      init.py
      models.py
   users/
      init.py
      models.py
   projects/
      init.py
      models.py
init.py
database.py (this is where the init_db was defined

至于为什么它看起来像这样的结构,原因是使项目以其形式模块化。

您需要models.py或models包的父目录位于sys.path中。您可以将它放在控制台会话中,或者在shell中,将它添加到环境变量PYTHON_PATH中,以便在进入控制台会话时设置它。

那么我该怎么做?非常抱歉,因为我刚刚接触过操作烧瓶。您的models.py或models包在哪里?如果它位于/foo/bar中,那么在解释器提示下,先导入sys,然后是sys.path.append'/foo/bar'。这样,导入模型就可以工作了,当然这不能保证以后不会有其他导入,这取决于您在哪里导入什么,以及您没有告诉我们的秘密,您的目录结构是什么-
app/
   filebase/
      init.py
      models.py
   users/
      init.py
      models.py
   projects/
      init.py
      models.py
init.py
database.py (this is where the init_db was defined