Python 2.7 已达到支持终止期限,并将于 2026 年 1 月 31 日
弃用。弃用后,您将无法部署 Python 2.7 应用,即使您的组织之前曾使用组织政策重新启用旧版运行时的部署也是如此。现有的 Python 2.7 应用在
弃用日期之后将继续运行并接收流量。我们建议您
迁移到最新支持的 Python 版本。
Python 2 模块配置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
appengine_config.py
文件是一个配置文件,可让您指定库的安装文件夹,以及为 google.appengine 软件包中的某些 Python 模块的常量和“钩子函数”提供您自己的值。如果您指定自己的值,则可以根据应用的需要更改相关 App Engine 服务的默认行为。您可以定义此文件和 app.yaml
配置文件,并将其与应用的其余代码一起部署。
使用 appengine_config.py 配置 Python 模块
App Engine 中的一些 Python 模块可以使用 appengine_config.py
进行配置。
如需自定义服务的 Python 模块,您可以在该服务的根目录中创建一个新的 appengine_config.py
文件。如需使用此文件,您只需定义您想要替换的常量或钩子函数。然后,从 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
中的钩子函数 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
进行配置。按照惯例,钩子函数使用小写字母,而常量使用大写字母:
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 模块
App Engine 还允许您使用 appengine_config.py
中定义的常量和钩子函数来配置您自己的 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
以及通过以下方式调用其钩子函数:
_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
使用。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]