Skip to content

Instantly share code, notes, and snippets.

@wasnot
Created March 3, 2017 09:16
Show Gist options
  • Save wasnot/d76436e9d840a77a8a8107170fe8294b to your computer and use it in GitHub Desktop.
Save wasnot/d76436e9d840a77a8a8107170fe8294b to your computer and use it in GitHub Desktop.
GAE/pyでgoogle-cloudライブラリを使用する際の注意点 ref: http://qiita.com/wasnot/items/9506e34afb38f70c7103
# ... 以下はpython sdkの場所が/usr/local/google_appengineのケース
/usr/local/google_appengine
import sys, types, os;has_mfs = sys.version_info > (3, 5);p = os.path.join('/usr/local/google_appengine', *('google',));importlib = has_mfs and __import__('importlib.util');has_mfs and __import__('importlib.machinery');m = has_mfs and sys.modules.setdefault('google', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('google', [os.path.dirname(p)])));m = m or not has_mfs and sys.modules.setdefault('google', types.ModuleType('google'));mp = (m or []) and m.__dict__.setdefault('__path__',[]);(p not in mp) and mp.append(p)
# ...
$ pip install google-cloud-datastore
from google.cloud import datastore
client = datastore.Client()
product_key = client.key('Product', 123)
print(client.get(product_key))
>>> import google.appengine
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named appengine
import sys, types, os
has_mfs = sys.version_info > (3, 5)
# google_appengineの中のgoogleモジュールのパスを作成
p = os.path.join('/usr/local/google_appengine', *('google',))
# Moduleオブジェクトを取り出す
# 3.5のケース
importlib = has_mfs and __import__('importlib.util')
has_mfs and __import__('importlib.machinery')
m = has_mfs and sys.modules.setdefault('google', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('google', [os.path.dirname(p)])))
# 2.7のケース
m = m or not has_mfs and sys.modules.setdefault('google', types.ModuleType('google'))
mp = (m or []) and m.__dict__.setdefault('__path__',[])
# pathをモジュールに追加
(p not in mp) and mp.append(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment