python-3.x - ModuleNotFoundError:没有名为“第一个”的模块
问题描述
我正在尝试在我的终端中创建一个数据库,但是当我尝试它时,我总是在使用时遇到此错误from first import db
:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'first'
这是我的代码
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///posts.db'
db = SQLAlchemy(app)
class BlogPost(db.Model):
id = db.Column(db.Integer, primary_key= True)
Title = db.Column(db.String(100), nullable= False)
Content = db.Column(db.Text, nullable= False)
Date_posted = db.Column(db.DateTime, nullable= False, default=datetime.utcnow)
def __repr__(self):
return "Blog Post" + str(self.id)
all_posts = [
{
'Title': 'Post 1',
'Content': "This is the first content"
},
{
'Title': 'Post 2',
'Content': 'This is the second content'
}
]
@app.route('/')
def index():
return render_template("index.html")
@app.route('/blog-posts')
def blog_posts():
return render_template("posts.html", posts = all_posts)
@app.route('/home')
def hello():
return "My first python website"
if __name__ == '__main__':
app.run(debug= True)
我只安装了 python 3.8。
可能是什么问题呢?我已经搜索了互联网并在其他平台上使用了所有建议,但没有解决方案。Flask 和 flask-sqlalchemy 已正确安装。
这是我在创建此项目时用作指南的 youtube 视频: https ://www.youtube.com/watch?v=3mwFC4SHY-Y
解决方案
好吧,我假设您已经创建了一个名为first
. 我列出了可能出错的地方:
- 您在错误的目录中。
- 您尚未安装必要的软件包。(不太可能)
- 根据教程,文件名是
app.py
他在那里执行from app import db
的。只需确保您的文件名正确。
我想不出任何其他可能的原因,除非first
您创建了一个包来存储项目的 python 文件,如routes.py
,models.py
等。在这种情况下,您需要__init__.py
在包目录中有文件并初始化app
并db
在__init__.py
文件中
推荐阅读
- regex - 如何使用正则表达式提取令牌值?
- excel - 比较两个不同大小的表,在 VBA 中有多个列
- python - 如何在 Python 中为网格数据设置边界?(netCDF 文件格式)
- c++ - 调用 powershell 脚本时未使用 c++ 系统 api 设置 Errno
- apache-kafka - 使用 Spring Cloud Stream 的 Kafka Sticky Partitioner
- visual-studio-code - 为什么 stylelint vscode 扩展在我的电脑上不起作用?
- python - Flask 动态路由采用我未指定的值
- c - C中的堆栈与程序变量地址
- python - 如何在 DataFrame 中根据其特征重要性映射特征?
- python-3.x - python Tensorflow 2.4.0'输入必须是4维[1,1,371,300,3]'错误