Flask通过Pycharm导入文件

问题描述 投票:0回答:1

关于Flask的微博教程:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

在Pycharm中,无论我如何构造或命名文件,如果我分离代码并导入文件,我都无法运行dev服务器。无论我在哪里移动init,视图,运行文件,我都无法继承代码。让我运行服务器的唯一方法是让所有命令在同一个文件上执行。我究竟做错了什么?

我把它设置为:项目1>应用程序(目录)> tmp(目录)> run.py(文件)

app(目录)> static(目录)> templates(目录)> init.py(文件)> views.py(文件)(我尝试了不同的安排。)

在views.py中:来自app import app

在run.py中:来自app import app

在init.py中:来自应用程序导入视图的烧瓶导入Flask

(我尝试了许多不同的组合,例如从app import app.views。从app import视图作为app_views。我也尝试重命名目录/文件,没有任何工作。)

python flask pycharm
1个回答
0
投票
  1. 使用PyCharm构建新项目,它将为您创建一个虚拟环境。然后将这些放入你的项目的根目录中的run.py中(不要忘记在prod中关闭调试模式) from app import create_app app = create_app() if __name__ == '__main__': app.run(debug=True) 在'app'中设置init.py文件: def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(Config) db.init_app(app) bcrypt.init_app(app) login_manager.init_app(app) mail.init_app(app) 将您的凭据存储到Config类: class Config:SECRET_KEY ='你的密钥......'SQLALCHEMY_DATABASE_URI ='你的数据库......'SQLALCHEMY_TRACK_MODIFICATIONS = False MAIL_SERVER ='smtp.google.com'MAIL_PORT = 587 MAIL_USE_TLS =真MAIL_USERNAME ='你的邮箱'MAIL_PASSWORD ='电子邮件密码“ 构建项目,将空的init.py放入每个目录(相应于您的体系结构)。下面是一个示例,如何在Flask中构建项目。它运行没有问题 . ├── README.md ├── app │   ├── __init__.py │   ├── config.py │   ├── errors │   │   ├── 403.html │   │   ├── 404.html │   │   ├── 500.html │   │   ├── __init__.py │   │   └── handlers.py │   ├── main │   │   ├── __init__.py │   │   └── routes.py │   ├── models.py │   ├── posts │   │   ├── __init__.py │   │   ├── forms.py │   │   └── routes.py │   ├── site.db │   ├── static │   │   ├── main.css │   │   └── profile_pics │   │   ├── 3c4feb2bb50d90df.png │   │   ├── ba3d328163a8125e.png │   │   └── default.jpg │   ├── templates │   │   ├── about.html │   │   ├── account.html │   │   ├── create_post.html │   │   ├── home.html │   │   ├── layout.html │   │   ├── login.html │   │   ├── post.html │   │   ├── register.html │   │   ├── reset_request.html │   │   ├── reset_token.html │   │   └── user_posts.html │   └── users │   ├── __init__.py │   ├── forms.py │   ├── routes.py │   └── utils.py └── run.py
© www.soinside.com 2019 - 2024. All rights reserved.