{"id":205242,"date":"2025-05-29T16:00:12","date_gmt":"2025-05-29T08:00:12","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205242\/"},"modified":"2025-05-29T16:00:12","modified_gmt":"2025-05-29T08:00:12","slug":"flask-sqlalchemy-%e6%a8%a1%e5%9d%97%e5%8c%96%e4%bd%bf%e7%94%a8%e6%97%b6%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8dnexpect-system-error-the-setup-method-shell_context_processor-can-no-longe","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205242\/","title":{"rendered":"Flask-SQLAlchemy \u6a21\u5757\u5316\u4f7f\u7528\u65f6\u5982\u4f55\u907f\u514d\u201cnexpect System Error &#8211; The setup method \\&#8217;shell_context_processor\\&#8217; can no longer be called on the application\u201d\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Flask-SQLAlchemy \u6a21\u5757\u5316\u4f7f\u7528\u65f6\u5982\u4f55\u907f\u514d\u201cnexpect System Error &#8211; The setup method \\&#8217;shell_context_processor\\&#8217; can no longer be called on the application\u201d\u9519\u8bef\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241127\/173266943067466ff630909.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5b98\u65b9\u6587\u6863\u4e2d\u7684\u4f8b\u5b50\u662f\u5c06\u6570\u636e\u5e93\u521d\u59cb\u5316\u4ee3\u7801\u653e\u5728 app.py \u4e2d\uff0c\u4f46\u5b9e\u9645\u9879\u76ee\u4e2d\u901a\u5e38\u4f1a\u5c06\u8fd9\u4e9b\u4ee3\u7801\u653e\u5728\u5355\u72ec\u7684\u6a21\u5757\u4e2d\u3002\u5728\u8fd9\u6837\u7684\u60c5\u51b5\u4e0b\uff0c\u5c06\u6a21\u5757\u5f15\u5165\u540e\u53ef\u80fd\u4f1a\u51fa\u73b0\u4ee5\u4e0b\u9519\u8bef\uff1a<\/p>\n<pre>nexpect system error - the setup method 'shell_context_processor' can no longer be called on the application. it has already handled its first request, any changes will not be applied consistently.\\nmake sure all imports, decorators, functions, etc. needed to set up the application are done before running it.<\/pre>\n<p>\u8fd9\u4e2a\u9519\u8bef\u8868\u660e current_app \u4e2d\u7684\u6570\u636e\u5e93\u8bbe\u7f6e\u5df2\u7ecf\u5b8c\u6210\uff0c\u63a5\u4e0b\u6765\u5f15\u5165\u7684\u4ee3\u7801\u65e0\u6cd5\u518d\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u3002\u89e3\u51b3\u65b9\u6cd5\u662f\u5728\u5916\u90e8\u6a21\u5757\u4e2d\u4f7f\u7528\u81ea\u5df1\u7684 app \u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4f7f\u7528 current_app\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u5916\u90e8\u6a21\u5757 a.py \u4e2d\u521d\u59cb\u5316\u6570\u636e\u5e93\uff1a<\/p>\n<pre>from flask import flask\nfrom flask_sqlalchemy import sqlalchemy\n\napp = flask(__name__)\nsetting = app.config[\"database\"]\napp.config[\"sqlalchemy_database_uri\"] = f'mysql+pymysql:\/\/{setting[\"db_user\"]}:{setting[\"db_pass\"]}@{setting[\"db_host\"]}\/{setting[\"db_name\"]}'\n\ndb = sqlalchemy(app)<\/pre>\n<p>\u7136\u540e\uff0c\u5728\u4f7f\u7528\u6570\u636e\u5e93\u7684\u6a21\u5757 b.py \u4e2d\uff0c\u5f15\u5165 a.py \u4e2d\u7684\u6570\u636e\u5e93\u5bf9\u8c61\uff1a<\/p>\n<pre>from a import DB\n\nclass Account:\n\n    @staticmethod\n    def login(username, password):\n        user = DB.session.execute(DB.select(User).filter_by(name=username)).scalar_one()\n\n        return \"token\"<\/pre>\n<p>\u8fd9\u6837\uff0c\u5728\u5916\u90e8\u6a21\u5757\u4e2d\u4e5f\u53ef\u4ee5\u987a\u5229\u4f7f\u7528\u6570\u636e\u5e93\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aFlask-SQLAlchemy \u6a21\u5757\u5316\u4f7f\u7528\u65f6\u5982\u4f55\u907f\u514d\u201cnexpect System Error &#8211; The setup method \\&#8217;shell_context_processor\\&#8217; can no longer be called on the application\u201d\u9519\u8bef\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Flask-SQLAlchemy&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205242","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205242"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205242\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}