Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

GoogleAppEngine python3 HelloWorldプログラム

Last updated at Posted at 2019-12-30

環境

windows10
GoogleAppEngine (python3.7)
Gits
anaconda3 (python3.6)

#参考
Google App Engine/Python で無料でAIサービスをホストする
参考 ★https://qiita.com/jiny2001/items/2180c4ddc5cc493b0cae

App Engine スタンダード環境での Python 3 のクイックスタート
に従ってインストールから python3 webアプリ実行まで

https://cloud.google.com/appengine/docs/standard/python3/quickstart?hl=ja

検索ワード flask python google app engine
でこれから調べて自分でイチから作成できるようにしたい

使用課金について(無料ではないので注意! これから調べる)

・1年間無料枠はあるが、それ超えたら課金される

・↓常時無料枠もある?
Google Cloud Platform 2つの料金無料枠
https://qiita.com/rache/items/796a65a446cc2788144a

プロジェクト asia2(Tokyo)とかだと無料じゃないっぽい
プロジェクト作成コマンド打つ時、リージョンを下記にしないと課金される?
米国東部(サウスカロライナ) us-east1
米国西部(オレゴン) us-west1
米国中央部(アイオワ) us-central1

いつでも無料!Google Compute Engine 常時無料枠の使い方
https://blog.apar.jp/web/6966/

プロジェクト作成

※プロジェクト名をhelloworldsakura にした。
helloworldとかだと、すでにあるプロジェクトだからダメのエラーメッセージだった。。

C:\Program Files (x86)\Google\Cloud SDK>gcloud projects create helloworldsakura

↓メッセージとして下記が表示される。
Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/helloworldsakura].
Waiting for [operations/cp.9026828903749621083] to finish...done.
Enabling service [cloudapis.googleapis.com] on project [helloworldsakura]...
Operation "operations/acf.f8dad4c6-f6ef-4405-a29d-c3821014b202" finished successfully.

作成されたプロジェクトの確認

C:\Program Files (x86)\Google\Cloud SDK>gcloud projects describe helloworldsakura

↓メッセージが表示される。
createTime: '2019-12-30T05:08:04.086Z'
lifecycleState: ACTIVE
name: helloworldsakura
projectId: helloworldsakura
projectNumber: '963795390246'

・App Engine アプリを初期化

C:\Program Files (x86)\Google\Cloud SDK>gcloud app create --project=helloworldsakura
↓メッセージが表示される。
You are creating an app for project [helloworldsakura].
WARNING: Creating an App Engine application for a project is irreversible and the region
cannot be changed. More information about regions is at
<https://cloud.google.com/appengine/docs/locations>.

Please choose the region where you want your App Engine application
located:

 [1] asia-east2    (supports standard and flexible)
 [2] asia-northeast1 (supports standard and flexible)
 [3] asia-northeast2 (supports standard and flexible)
 [4] asia-south1   (supports standard and flexible)
 [5] australia-southeast1 (supports standard and flexible)
 [6] europe-west   (supports standard and flexible)
 [7] europe-west2  (supports standard and flexible)
 [8] europe-west3  (supports standard and flexible)
 [9] europe-west6  (supports standard and flexible)
 [10] northamerica-northeast1 (supports standard and flexible)
 [11] southamerica-east1 (supports standard and flexible)
 [12] us-central    (supports standard and flexible)
 [13] us-east1      (supports standard and flexible)
 [14] us-east4      (supports standard and flexible)
 [15] us-west2      (supports standard and flexible)
 [16] cancel
↓ 2 asia-northeast1(Tokyo)を選ぶ
Please enter your numeric choice:  2

↓メッセージが表示される。
Creating App Engine application in project [helloworldsakura] and region [asia-northeast1]....done.
Success! The app is now created. Please use `gcloud app deploy` to deploy your first app.

Python 3.7 用の App Engine 拡張機能が含まれる gcloud コンポーネントをインストール。
※GoogleCloudSDKShellのアイコン左クリック「管理者として実行」で
起動することが必須

gcloud components install app-engine-python
※ ↓メッセージが表示される
C:\Program Files (x86)\Google\Cloud SDK>gcloud components install app-engine-python

Restarting command:
↓※ 別windowが開いてメッセージが表示される
  $ gcloud components install app-engine-python

Your current Cloud SDK version is: 274.0.1
Installing components from version: 274.0.1

+---------------------------------------------------+
|        These components will be installed.        |
+------------------------------+---------+----------+
|             Name             | Version |   Size   |
+------------------------------+---------+----------+
| Cloud Datastore Emulator     |   2.1.0 | 18.4 MiB |
| gRPC python library          |         |          |
| gRPC python library          |  1.20.0 |  1.5 MiB |
| gcloud app Python Extensions |  1.9.87 |  6.0 MiB |
+------------------------------+---------+----------+

For the latest full release notes, please visit:
  https://cloud.google.com/sdk/release_notes

Do you want to continue (Y/n)?  y

#============================================================#
#= Creating update staging area                             =#
#============================================================#
#= Installing: Cloud Datastore Emulator                     =#
#============================================================#
#= Installing: gRPC python library                          =#
#============================================================#
#= Installing: gRPC python library                          =#
#============================================================#
#= Installing: gcloud app Python Extensions                 =#
#============================================================#
#= Creating backup and activating new installation          =#
#============================================================#

Performing post processing steps...done.

Update done!

#サンプルプログラムのダウンロード
gitsをインストールしていることが前提
gits Bash を起動して、適当な場所にプログラムを持ってくる

$ cd /c/kuroda/work_google_GAE

user@DESKTOP-AH8EQM2 MINGW64 /c/kuroda/work_google_GAE

$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples
↓メッセージが表示される。
Cloning into 'python-docs-samples'...
remote: Enumerating objects: 25282, done.
remote: Total 25282 (delta 0), reused 0 (delta 0), pack-reused 25282
Receiving objects: 100% (25282/25282), 39.82 MiB | 1.54 MiB/s, done.
Resolving deltas: 100% (13367/13367), done.
Checking out files: 100% (2066/2066), done.

#サンプルプログラムのローカル実行(確認まで)
anaconda promptを起動して、pythonを実行する。※GoogleSDKShellではない

cd python-docs-samples/appengine/standard_python37/hello_world
python main.py

ブラウザで http://localhost:8080 にアクセスする

Web上で見ることができるようにする

anaconda promptを起動して、pythonを実行する。※GoogleSDKShellではない

cd python-docs-samples/appengine/standard_python37/hello_world
pip install  -r requirements.txt

GoogleSDKShellを起動する

cd python-docs-samples/appengine/standard_python37/hello_world

gcloud config set project helloworldsakura

↓メッセージが表示される。
Updated property [core/project].

gcloud app deploy

↓メッセージが表示される。
Services to deploy:

descriptor:      [C:\kuroda\work_google_GAE\python-docs-samples\appengine\standard_python37\hello_world\app.yaml]
source:          [C:\kuroda\work_google_GAE\python-docs-samples\appengine\standard_python37\hello_world]
target project:  [helloworldsakura]
target service:  [default]
target version:  [20191230t155314]
target url:      [https://helloworldsakura.appspot.com]


Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\lib\logging\__init__.py", line 861, in emit
    msg = self.format(record)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\lib\logging\__init__.py", line 734, in format
    return fmt.format(record)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\lib\googlecloudsdk\core\log.py", line 337, in format
    msg = super(_LogFileFormatter, self).format(record)
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\lib\logging\__init__.py", line 465, in format
    record.message = record.getMessage()
  File "C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\platform\bundledpython\lib\logging\__init__.py", line 329, in getMessage
    msg = msg % self.args
UnicodeDecodeError: 'ascii' codec can't decode byte 0x8e in position 10: ordinal not in range(128)
Logged from file context_util.py, line 386
#============================================================#
#= Uploading 5 files to Google Cloud Storage                =#
#============================================================#
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://helloworldsakura.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse

↑メッセージで表示された URLをブラウザで表示するとWEB上で見れるようになっている。
target url: [https://helloworldsakura.appspot.com]

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?