PyramidでToDo管理サービスを作る 【プロジェクト作成】 (リトライ編)

前回の記事で作成したプロジェクトだと、データベースが設定されていないものだったので再度プロジェクトを作成する。

neoinal.hatenablog.com

内容としては、「プロジェクトの作成からサーバの起動」からとなる。

プロジェクトの作成からサーバの起動

  1. scaffoldを使ってプロジェクトを作成

     (pyramid_python)> pcreate -s alchemy ToDoManager
    

    実行するといろんなファイルが作成される。ディレクトリ構成としてはこんな感じ。
    前回のディレクトリ構成と比較すると、 scripts ディレクトリや models.py などが増えている。

     E:.
     │  .gitignore
     │  CHANGES.txt
     │  development.ini
     │  LICENSE
     │  MANIFEST.in
     │  production.ini
     │  README.md
     │  README.txt
     │  setup.py
     │
     └─todomanager
         │  models.py
         │  tests.py
         │  views.py
         │  __init__.py
         │
         ├─scripts
         │  │  initializedb.py
         │  │  __init__.py
         │  │
         │  └─__pycache__
         ├─static
         │      pyramid-16x16.png
         │      pyramid.png
         │      theme.css
         │      theme.min.css
         │
         ├─templates
         │      mytemplate.pt
         │
         └─__pycache__
    
  2. setup.py の修正

    前回の失敗を繰り返さないために setup.py を修正する。

     with open(os.path.join(here, 'README.txt')) as f:
                                          ↓
     with open(os.path.join(here, 'README.md'), encoding='utf-8') as f:
    
  3. 作成したプロジェクトを実行

    必要なモジュールをインストールする。

     (pyramid_python)> python setup.py develop
    

    起動させるために次のコマンドを入力する。

     (pyramid_python)> pserve development.ini
    

    だがしかし、以下のエラーが出力される。
    どうやらデータベースの初期化が済んでないとのこと。

    Pyramid is having a problem using your SQL database. The problem might be caused by one of the following things:

    1. You may need to run the "initialize_ToDoManager_db" script to initialize your database tables. Check your virtual environment's "bin" directory for this script and try to run it.

    2. Your database server may not be running. Check that the database server referred to by the "sqlalchemy.url" setting in your "development.ini" file is running.

    After you fix the problem, please restart the Pyramid application to try it again.

    コマンドを叩いてデータベースを初期化する。
    デフォルトの設定のままなので、SQLiteが使用される。

     (pyramid_python)> initialize_ToDoManager_db development.ini
    

    このままだと、一緒に .sqlite ファイルもコミットする可能性があるので .gitignore に追加する。

     # Database
     *.sqlite
    

    気を取り直して再度起動する。

     (pyramid_python)> pserve development.ini
    

    以下のログが出力されて起動する。

     Starting server in PID 4192.
     serving on http://0.0.0.0:6543
    

    ブラウザで「http://localhost:6543」にアクセスすると前回と同じ画面が表示される。

リポジトリの方は、消さずに不要なファイルを消して上書きしておこう。