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 では、フック関数 default_namespace_for_request
を appengine_config.py
内で次のようにオーバーライドできます。
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()
プログラマによっては、デフォルトを 1 つのクラスにまとめる場合もあります。
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 Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[[["わかりやすい","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 UTC。"],[[["\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."]]