如何在另一个文件中实例化我的变量
大家好,我正在尝试做一些非常简单的事情,但所有文档都在同一个文件上执行此操作。
我想在我的flask应用程序中使用oauth,这是我的main.py文件
from flask import Flask
from login_routes import login
from authlib.integrations.flask_client import OAuth
app = Flask(__name__)
oauth = OAuth(app)
app.register_blueprint(login)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
,我的login_routes.py有这些行
from flask import Blueprint, render_template, url_for, redirect, session
from authlib.integrations.flask_client import OAuth
import settings
# OAuth config
google = oauth.register(
name='google',
client_id=settings.GOOGLE_CLIENT_ID,
client_secret=settings.GOOGLE_CLIENT_SECRET,
access_token_url=settings.GOOGLE_TOKEN_URI,
access_token_params=None,
authorize_url=settings.GOOGLE_AUTH_URI,
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'openid profile email'}
)
但是oauth没有在我的login_routes.py中声明,我不知道如何将该变量从主实例带到另一个实例文件中,我尝试导入 main.py ,这在我的大脑中毫无意义,因为最终会出现循环引用错误,请让我知道如何正确实例化它。
Hello everyone I'm trying to do something very simple but all the documentation out do this on the same file.
I want to use oauth in my flask app and this is my main.py file
from flask import Flask
from login_routes import login
from authlib.integrations.flask_client import OAuth
app = Flask(__name__)
oauth = OAuth(app)
app.register_blueprint(login)
if __name__ == '__main__':
app.run(host='127.0.0.1', port=8080, debug=True)
And my login_routes.py has these lines
from flask import Blueprint, render_template, url_for, redirect, session
from authlib.integrations.flask_client import OAuth
import settings
# OAuth config
google = oauth.register(
name='google',
client_id=settings.GOOGLE_CLIENT_ID,
client_secret=settings.GOOGLE_CLIENT_SECRET,
access_token_url=settings.GOOGLE_TOKEN_URI,
access_token_params=None,
authorize_url=settings.GOOGLE_AUTH_URI,
authorize_params=None,
api_base_url='https://www.googleapis.com/oauth2/v1/',
client_kwargs={'scope': 'openid profile email'}
)
However oauth is not declared in my login_routes.py, I dont know how to bring that variable from main instance to this other file, I tried importing main.py which on my brain makes no sense because that ends up on a circular reference error please let me know how to instantiate this correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
在 main.py 中的
login_routes.py 中
Try
In main.py
In login_routes.py