LoginSignup
4
4

More than 5 years have passed since last update.

GAE pythonでモジュールの読み込み先の指定

Last updated at Posted at 2015-06-04

GAE上でFlask等既存モジュールを利用する際のライブラリ置き場の作成と読み込み(import)を自然な感じでやる

app.yamlと同階層に、main.pyとlibフォルダーを作成してあり、main.pyから、各種ライブラリーをimportしたいとき

root_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(root_dir, 'lib'))

を、import直前に書いておけばOK

sample

#!coding:utf8
import os
import sys
from datetime import date
from google.appengine.ext import ndb
from google.appengine.ext.webapp.util import run_wsgi_app

import csv
import json
import urllib
import datetime
from random import randint

root_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(root_dir, 'lib'))

import jinja2
from flask import Flask
from flask import request
from flask import jsonify
from flask import redirect
from flask import render_template

from models import MyModel
4
4
3

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
4
4