Python 2.7 已終止支援,並將於 2026 年 1 月 31 日
淘汰。淘汰後,您將無法部署 Python 2.7 應用程式,即使貴機構先前曾使用機構政策重新啟用舊版執行階段的部署作業,也無法部署。現有的 Python 2.7 應用程式在
淘汰日期過後,仍會繼續執行並接收流量。建議您
改用系統支援的最新 Python 版本。
Python 2 模組設定
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
appengine_config.py
檔案是設定檔,可讓您指定
程式庫的安裝資料夾,並為 google.appengine 套件中的部分 Python 模組提供常數和「hook 函式」的自訂值。指定自訂值可能會變更應用程式所需相關 App Engine 服務的預設行為。您可以與 app.yaml
設定檔一起定義這個檔案,並與應用程式的其他程式碼一起部署。
使用 appengine_config.py 設定 Python 模組
App Engine 中的多個 Python 模組可透過 appengine_config.py
加以設定。
如要自訂服務的 Python 模組,請在該服務的根目錄中建立新的 appengine_config.py
檔案。如要使用這個檔案,您必須定義要覆寫的常數或 hook 函式,接著,請從 app.yaml
檔案所在的目錄執行 gcloud app
deploy
,重新部署應用程式和新的 appengine_config.py
檔案。您定義的常數和掛鉤函式,就會由這些 App Engine 服務在內部使用。
如要覆寫常數,請在常數名稱前面加上 Python 模組名稱和底線,然後指派值。舉例來說,如要編輯 appstats 中的覆寫,您可以定義 KEY_PREFIX
的值:
appstats_KEY_PREFIX = '__my_custom_prefix__'
在其他 Python 模組中,覆寫的掛鉤函式命名方式也類似。舉例來說,在
namespace_manager 中,您可以將 appengine_config.py
中的 hook 函式 default_namespace_for_request
覆寫為:
import os
def namespace_manager_default_namespace_for_request():
return os.environ.get('HTTP_HOST', '')
App Engine 中可設定的 Python 模組
下列 Python 模組可使用 appengine_config.py
進行設定。按照慣例,hook 函式為小寫,常數為大寫:
namespace_manager
-
default_namespace_for_request()
(預設會傳回 None
)
appstats
datastore_admin
BASE_PATH
(預設為 '/_ah/datastore_admin'
)
MAPREDUCE_PATH
(預設為 '/_ah/mapreduce'
)
CLEANUP_MAPREDUCE_STATE
(預設為 True
)
remoteapi
透過 lib_config 設定自訂的 Python 模組
您也可以使用 appengine_config.py
中定義的常數和 hook 函式,透過 App Engine 設定自己的 Python 模組。
lib_config.register()
函式可讓您註冊使用者可覆寫的常數和鉤子的名稱,並在使用者不想覆寫時定義合理的預設值。在內部,lib_config.register()
會嘗試匯入 appengine_config
。如果成功,系統會將指定 Python 模組的預設值,替換為 appengine_config.py
中定義的值。
在 my_module.py
中的使用範例:
from google.appengine.api import lib_config
def _hook_function1_default():
return 'baz'
_config = lib_config.register('my_module', {'CONSTANT1': 'foo',
'CONSTANT2': 'bar',
'hook_function1': _hook_function1_default})
您現在可以將使用者的常數存取為:
_config.CONSTANT1
_config.CONSTANT2
並呼叫其 hook 函式為:
_config.hook_function1()
有些程式設計人員會將預設值集合為類別:
class _ConfigDefaults(object):
CONSTANT1 = 'foo'
CONSTANT2 = 'bar'
def hook_function1():
return 'baz'
_config = lib_config.register('my_module', _ConfigDefaults.__dict__)
如要覆寫預設值,使用者可以在 appengine_config.py
中定義:
my_module_CONSTANT1 = 'foofoo'
my_module_hook_function1 = lambda: 'bazbaz'
因此,在 my_module.py
中,以下敘述為真:
_config.CONSTANT1
現在是 'foofoo'
_config.CONSTANT2
仍為 'bar'
_config.hook_function1()
傳回 'bazbaz'
lib_config.register()
傳回之後,就可以立即在 my_module.py
中使用使用者覆寫值。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-09-04 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003eappengine_config.py\u003c/code\u003e file allows customization of App Engine services by defining constants and hook functions, which override default behaviors.\u003c/p\u003e\n"],["\u003cp\u003eYou can configure specific Python modules, such as \u003ccode\u003enamespace_manager\u003c/code\u003e, \u003ccode\u003eappstats\u003c/code\u003e, \u003ccode\u003edatastore_admin\u003c/code\u003e, and \u003ccode\u003eremoteapi\u003c/code\u003e, by creating an \u003ccode\u003eappengine_config.py\u003c/code\u003e file in your service's root directory.\u003c/p\u003e\n"],["\u003cp\u003eOverriding constants and hook functions involves prefixing their names with the Python module name and an underscore within the \u003ccode\u003eappengine_config.py\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003elib_config.register()\u003c/code\u003e function enables developers to make their own Python modules configurable, allowing users to override default constants and hook functions through \u003ccode\u003eappengine_config.py\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eChanges made in \u003ccode\u003eappengine_config.py\u003c/code\u003e are applied after deploying the application with \u003ccode\u003egcloud app deploy\u003c/code\u003e, ensuring that the customized settings are utilized by the App Engine services.\u003c/p\u003e\n"]]],[],null,["# Python 2 Module Configuration\n\nThe `appengine_config.py` file is a configuration file that provides you the\nability to specify the [installation folder for libraries](/appengine/docs/legacy/standard/python/tools/using-libraries-python-27#copying_a_third-party_library) and provide your own values for constants and\n\"hook functions\" for some of the Python modules in the [google.appengine](/appengine/docs/legacy/standard/python/refdocs/google.appengine) packages.\nSpecifying your own values can change the default behavior of the\nrelated App Engine services based on the application's needs. You define this file alongside your\n[`app.yaml` configuration\nfile](/appengine/docs/legacy/standard/python/config/appref) and deploy it with the rest of your app's code.\n\nConfiguring Python modules with appengine_config.py\n---------------------------------------------------\n\n\nSeveral [Python modules in\nApp Engine](#Configurable_Python_Modules_In_App_Engine) are configurable using `appengine_config.py`.\n\n\nTo customize the Python modules of your [services](/appengine/docs/legacy/standard/python/an-overview-of-app-engine),\nyou create a new `appengine_config.py` file in the root directory\nof that service. To use this file, you need to define only those constants or\nhook functions you wish to override. You then run `gcloud app\ndeploy` from the directory where the `app.yaml` file is\nlocated to redeploy your app along with the new\n`appengine_config.py` file. The constants and hook functions that\nyou defined will then be used by those App Engine services internally.\n\n\nTo override a constant, prefix the constant's name with the Python module\nname and an underscore, then assign a value. For example, to edit overrides in\n[appstats](/appengine/docs/legacy/standard/python/tools/appstats), you can\ndefine the value of `KEY_PREFIX` \n\n```python\nappstats_KEY_PREFIX = '__my_custom_prefix__'\n```\n\n\nNaming of overridden hook functions is similar in other Python modules. For\nexample, in [namespace_manager](/appengine/docs/legacy/standard/python/multitenancy/multitenancy), you can override the hook function\n`default_namespace_for_request` in `appengine_config.py`\nas follows: \n\n```python\nimport os\ndef namespace_manager_default_namespace_for_request():\n return os.environ.get('HTTP_HOST', '')\n```\n\nConfigurable Python modules in App Engine\n-----------------------------------------\n\n\nThe Python modules listed below are configurable using\n`appengine_config.py`. By convention, hook functions are lowercase\nand constants are uppercase:\n[namespace_manager](/appengine/docs/legacy/standard/python/multitenancy/multitenancy)\n\n- `default_namespace_for_request()` (default returns `None`)\n\n\u003cbr /\u003e\n\n[appstats](/appengine/docs/legacy/standard/python/tools/appstats)\n\n- see `class ConfigDefaults` in [appengine/ext/appstats/recording.py](/appengine/docs/legacy/standard/python/refdocs/google.appengine.ext.appstats.recording#google.appengine.ext.appstats.recording.ConfigDefaults)\n- see example in [appengine/ext/appstats/sample_appengine_config.py](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/appstats/sample_appengine_config)\n\n\u003cbr /\u003e\n\n[datastore_admin](/appengine/docs/python/console/managing-datastore)\n\n- `BASE_PATH` (default `'/_ah/datastore_admin'`)\n- `MAPREDUCE_PATH` (default `'/_ah/mapreduce'`)\n- `CLEANUP_MAPREDUCE_STATE` (default `True`)\n\n\u003cbr /\u003e\n\n[remoteapi](/appengine/docs/legacy/standard/python/tools/remoteapi)\n\n- `CUSTOM_ENVIRONMENT_AUTHENTICATION`\n- see `class ConfigDefaults` in [appengine/ext/remote_api/handler.py](/appengine/docs/legacy/standard/python/refdocs/google.appengine.ext.remote_api.handler#google.appengine.ext.remote_api.handler.ConfigDefaults)\n\n\u003cbr /\u003e\n\nConfiguring your own Python modules with lib_config\n---------------------------------------------------\n\n\nApp Engine also allows you to configure your own Python modules with constants\nand hook functions defined in `appengine_config.py`. The\n[`lib_config.register()` function](/appengine/docs/legacy/standard/python/refdocs/google.appengine.api.lib_config#google.appengine.api.lib_config.LibConfigRegistry.register) allows you to both register the\nnames of the user-overridable constants and hooks, and to define sensible\ndefaults in case the users don't wish to override them. Internally,\n`lib_config.register()` attempts to import\n`appengine_config`. If successful, it replaces the defaults of the\nspecified Python modules with those defined in\n`appengine_config.py`.\n\nExample usage in `my_module.py`: \n\n```python\nfrom google.appengine.api import lib_config\n\ndef _hook_function1_default():\n return 'baz'\n\n_config = lib_config.register('my_module', {'CONSTANT1': 'foo',\n 'CONSTANT2': 'bar',\n 'hook_function1': _hook_function1_default})\n```\n\n\nNow you can access a user's constants as: \n\n```python\n_config.CONSTANT1\n_config.CONSTANT2\n```\n\n\nand call their hook function as: \n\n```python\n_config.hook_function1()\n```\n\nSome programmers like to group their defaults into a class: \n\n```python\nclass _ConfigDefaults(object):\n CONSTANT1 = 'foo'\n CONSTANT2 = 'bar'\n def hook_function1():\n return 'baz'\n\n_config = lib_config.register('my_module', _ConfigDefaults.__dict__)\n```\n\n\nIn order to override your defaults, a user could define in\n`appengine_config.py`: \n\n```python\nmy_module_CONSTANT1 = 'foofoo'\nmy_module_hook_function1 = lambda: 'bazbaz'\n```\n\n\nAs a result, in `my_module.py`, the following will be true:\n\n- `_config.CONSTANT1` is now `'foofoo'`\n- `_config.CONSTANT2` remains `'bar'`\n- `_config.hook_function1()` returns `'bazbaz'`\n\n\nThe user overrides are available to `my_module.py` immediately\nafter `lib_config.register()` returns."]]