Hope is a Dream. Dream is a Hope.

非公開ふぃふぃ工房ブログは再開しました。

2018年度版AWS CodeStarでFlask環境構築方法とハマリポイント

1. Python3の利用

(追記) 原因が分かった.CodeBuildの環境がpython2.7(デフォルト)になっている. CodeBuildの「プロジェクトの編集」にて,使用するイメージを変更できる!

=> デフォルトでは仮想環境にてPython3がインストールされている => しかし, cloud9ではaliasでpython=python27と設定されているため,python27が起動される => (対策) unalias python を実行することで,python=venv/bin/python3が起動される => (補足) デプロイされるサーバーにてaliasが設定されているかは分からないが,念のためインストールスクリプトにもunaliasを追加しておく

file : install_dependenceies

# Install Python 
virtualenvenvironment
source environment/bin/activate
pip install -r requirements.txt
pip install supervisor
python setup.py install

下に変更

# Install Python 
unalias python       // (追加) いらないかも
virtualenv -p python3.6 environment // (ヴァージョンの指定を追加) いらないかも
source environment/bin/activate
pip install -r requirements.txt
pip install supervisor
python setup.py install

デプロイ先でpytho2になっている可能性があるので,テストにヴァージョンを追加しておく

import json
import pytest

def test_python_version():
    import sys
    ver = sys.version_info;
    assert ver.major == 3

2. Flask + Python3の利用

2.1 プロジェクト名の変更

/scripts/supervisord.conf

[program:flaskapplication]
- command = /home/ec2-user/python-flask-service/environment/bin/python /home/ec2-user/python-flask-service/helloworld/application.py
+ command = /home/ec2-user/python-flask-service/environment/bin/python /home/ec2-user/python-flask-service/yourapp/application.py

buildspec.yml

version: 0.2

phases:
  pre_build:
    commands:
      # Discover and run unit tests. For more information, see <https://docs.pytest.org/en/latest/goodpractices.html>
      - python setup.py pytest

artifacts:
  type: zip
  files:
    - 'template.yml'
    - 'scripts/**/*'
-   - 'helloworld/**/*.py'
+   - 'yourapp/**/*.py'
    - 'appspec.yml'
    - 'requirements.txt'
    - 'setup.py'

setup.py

from setuptools import setup, find_packages

setup(
-   name='helloworld',
+   name='yourapp',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'flask',
    ],
    setup_requires=[
        'pytest-runner',
    ],
    tests_require=[
        'pytest',
    ],
)

3. Flask + Blueプリントで, APIとFrontを分ける

yourapplication/ app.py /backend/ init.py reset_api.py /frontend/ init.py views.py /static/ /templates/

いまさらながら Flask についてまとめる 〜Blueprint〜 - 適当おじさんの適当ブログ Modular Applications with Blueprints — Flask 1.0.2 documentation

  1. [CodeStar] でFlask(EC2)を立ち上げる
  2. [CodeBuild] プロジェクト設定を編集して,python3.6系ランタイムに変更
  3. [Cloud9] にてアプリの名前を変更

3.1 setup.py

from setuptools import setup, find_packages

setup(
-   name='helloworld',
+   name='hebel',
    packages=find_packages(),
    include_package_data=True,
    install_requires=[
        'flask',
    ],
    setup_requires=[
        'pytest-runner',
    ],
    tests_require=[
        'pytest',
    ],
)

3.2 buildspec.yml

version: 0.2

phases:
  pre_build:
    commands:
      # Discover and run unit tests. For more information, see <https://docs.pytest.org/en/latest/goodpractices.html>
      - python setup.py pytest

artifacts:
  type: zip
  files:
    - 'template.yml'
    - 'scripts/**/*'
-   - 'helloworld/**/*.py'
+   - 'hebel/**/*.py'
    - 'appspec.yml'
    - 'requirements.txt'
    - 'setup.py'

3.3 ./helloworld/ => ./hebel/

import json
import pytest
from hebel.application import application

@pytest.fixture
def client():
    return application.test_client()

def test_response(client):
    result = client.get()
    response_body = json.loads(result.get_data())
    assert result.status_code == 200
    assert result.headers['Content-Type'] == 'application/json'
    assert response_body['Output'] == 'Hello World'

3.4 tests/test_application.py

import json
import pytest
from helloworld.application import application

@pytest.fixture
def client():
    return application.test_client()

def test_response(client):
    result = client.get()
    response_body = json.loads(result.get_data())
    assert result.status_code == 200
    assert result.headers['Content-Type'] == 'application/json'
    assert response_body['Output'] == 'Hello World'

    ```



4. .gitignoreを追加
デフォルトでは.gitignoreが無いので. gitに色々コミットされていやなので.
.egg, .venv, ....

curl https://www.gitignore.io/api/python > .gitignore


4. 仮想環境の構築

cd flask-blueprint virtualenv -p python3 .venv source .venv/bin/activate python -V #3.6系であることを確認する

もし2.7系であれば unalias python

$ which python ~/environment/flask-blueprint/.venv/bin/python

$ python -V Python 3.6.5

5. ひとまずテスト

tests/test_applicaiton.py

import json import pytest from hebel.application import application

@pytest.fixture def client(): return application.test_client()

def test_response(client): result = client.get() response_body = json.loads(result.get_data()) assert result.status_code == 200 assert result.headers['Content-Type'] == 'application/json' assert response_body['Output'] == 'Hello World'

念のために追加しておく

def test_python_version(): import sys ver = sys.version_info; assert ver.major == 3

テストの実行

python setup.py pytest

~省略~

=========================================================================================================================== test session starts =========================================================================================================================== platform linux -- Python 3.6.5, pytest-3.8.1, py-1.6.0, pluggy-0.7.1 rootdir: /home/ec2-user/environment/flask-blueprint, inifile: collected 1 item

tests/test_application.py . [100%]

======================================================================================================================== 1 passed in 0.11 seconds =========================================================================================================================





# Cloud9でのpython alias

もし, pythonが2.7の場合にはaliasがされているかもしれないので,念のためにaliasを消しておく

unalias python

Cloud9でVirtualenvを使ってPython3が使えない

nohara:~/environment/tmp $ virtualenv -p 3.6 venv36 The path 3.6 (from --python=3.6) does not exist nohara:~/environment/tmp $ virtualenv -p python3.6 venv36 Already using interpreter /usr/bin/python3.6 Using base prefix '/usr' New python executable in /home/ec2-user/environment/tmp/venv36/bin/python3.6 Not overwriting existing python script /home/ec2-user/environment/tmp/venv36/bin/python (you must use /home/ec2-user/environment/tmp/venv36/bin/python3.6) Installing setuptools, pip, wheel...done. nohara:~/environment/tmp $ which python /usr/bin/python nohara:~/environment/tmp $ python -V Python 2.7.14 nohara:~/environment/tmp $ source venv36/bin/activate (venv36) nohara:~/environment/tmp $ vhich python bash: vhich: command not found (venv36) nohara:~/environment/tmp $ which python
~/environment/tmp/venv36/bin/python (venv36) nohara:~/environment/tmp $ python -V Python 3.6.5 (venv36) nohara:~/environment/tmp $




## おまけ

git-commit.sh
debug用

cd /home/ec2-user/environment/python3-test git config --global user.name "T.Nohara" git config --global user.email nohara@example.com git add . git commit -m "auto commit" git push

1. Create a Python virtual environment for your Django project. This virtual
        $ unalias python
        $ virtualenv -p python3 .venv

2. Activate the virtual environment:

        $ source .venv/bin/activate

3. Install Python dependencies for this project:

        $ pip install -r requirements.txt

4. Install the sample application code into your virtual environment:

        $ python setup.py install

5. Start the Flask development server:

        $ python helloworld/application.py --port 8000