我目前正在编写Corey Schafer编写的Python Flask教程。在蓝图视频中,我在导出变量时遇到了麻烦。我已经写过~/.bash_profile
和~/.profile
了,但是没有成功。
在我的.profile文件中:
export SECRET_KEY='5791628bb0b13ce0c676dfde280ba245'
export SQLALCHEMY_DATABASE_URI='sqlite:///sitetest.db'
export EMAIL_USER='email'
export EMAIL_PASS='password'
在我的config.py文件中:
import os
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.environ.get('SQLALCHEMY_DATABASE_URI')
MAIL_SERVER = 'smtp.gmail.com'
MAIL_PORT = 587
MAIL_USE_TLS = True
MAIL_USERNAME = os.environ.get('EMAIL_USER')
MAIL_PASSWORD = os.environ.get('EMAIL_PASS')
在我的 init .py文件中
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flaskblog.config import Config
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager= LoginManager(app)
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail(app)
from flaskblog.users.routes import users
from flaskblog.posts.routes import posts
from flaskblog.main.routes import main
app.register_blueprint(users)
app.register_blueprint(posts)
app.register_blueprint(main)
我已经弄清楚,很可能没有导出变量。有什么建议吗?
Traceback (most recent call last):
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/michael/Flask_Blog/flaskblog/main/routes.py", line 11, in home
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 519, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/home/michael/anaconda3/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
return self.registry()
File "/home/michael/anaconda3/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1012, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/home/michael/anaconda3/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 3206, in __call__
return self.class_(**local_kw)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 138, in __init__
bind = options.pop('bind', None) or db.engine
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 937, in engine
return self.get_engine()
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 956, in get_engine
return connector.get_engine()
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 560, in get_engine
options = self.get_options(sa_url, echo)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 575, in get_options
self._sa.apply_driver_hacks(self._app, sa_url, options)
File "/home/michael/anaconda3/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 877, in apply_driver_hacks
if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'
答案 0 :(得分:1)
您是否在烧瓶项目中使用虚拟环境 ?下面的解决方案只适合您。
因此,当您使用虚拟环境时,它会以不同的安装软件包特权在自己的环境中工作。同样适用于环境变量。当您在bash_profile中添加环境变量并运行项目(使用虚拟环境)时,根本找不到这些变量。解决方法是..
在虚拟环境路径中创建自己的变量文件(例如, var_file ;如下所示) 。请参阅 terminal 中的以下命令,以获取更清晰的说明。
> vim ~/.virtualenvs/your_project_virtual_environment_name/bin/var_file
此处将打开外壳文件
# add your environment variables
export abc='xyz'
export xyz='wtf'
现在退出文件(esc +:+ wq!)
> source ~/.virtualenvs/your_project_virtual_environment_name/bin/new_shell_file
Voila .. !!立即尝试运行项目。它应该像蜂蜜一样光滑。 让我知道是否有效。 :)
答案 1 :(得分:0)
您需要为SQLALCHEMY_DATABASE_URI
环境变量设置适当的DSN。根据文档,它必须具有有效的方言前缀,例如mysql
,postgresql
或sqlite
:
https://docs.sqlalchemy.org/en/13/core/engines.html#database-urls
sa_url.drivername.startswith('mysql'):
堆栈跟踪中的这一行表示您提供的DSN最有可能缺少驱动程序名称。
确保os.environ.get('SQLALCHEMY_DATABASE_URI')
返回有效的DSN。
答案 2 :(得分:0)
我正在阅读同一教程,并且对此也做了一些努力。
我尝试添加assert
语句以查看哪个环境变量返回None。 SECRET_KEY
和DATABASE_URI
都给我带来麻烦。我尝试更改变量名称,甚至删除引号。
我不太确定哪个更改可以解决问题,但是我可以通过重新启动终端使环境变量正常工作。
我希望这对以后的人有帮助。