我的 Google App Engine 应用程序中的以下行(webapp.py
)无法导入谷歌云 https://googlecloudplatform.github.io/google-cloud-python/图书馆:
from google.cloud import storage
出现以下错误:
ImportError: No module named google.cloud.storage
我做了一些研究,发现以下文章很有帮助:
- https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_library https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27#installing_a_library
- https://stackoverflow.com/a/34585485 https://stackoverflow.com/a/34585485
- https://www.simonmweber.com/2013/06/18/python-protobuf-on-app-engine.html https://www.simonmweber.com/2013/06/18/python-protobuf-on-app-engine.html
结合使用上述文章建议的技术,我执行了以下操作:
-
创建一个requirements.txt
file:
google-cloud==0.19.0
-
使用导入此库pip
:
pip install -t lib -r requirements.txt
-
在我的中使用以下代码appengine_config.py
file:
import os
import sys
import google
libDir = os.path.join(os.path.dirname(__file__), "lib")
google.__path__.append(os.path.join(libDir, "google"))
sys.path.insert(0, libDir)
谁能告诉我为了让这个工作我可能会缺少什么?我只是想编写一个可以从 Google Cloud Storage 写入/读取的 Google App Engine 应用程序,并且我想在部署之前在本地进行测试。
看起来唯一需要的就是包括google-cloud
进入你的项目requirements.txt
file.
检查这个简单的示例是否适合您(您不应该收到任何导入错误)。
创建以下文件并运行pip install -r requirements.txt -t lib
。我的网站不需要再做任何事情就可以正常工作。
app.yaml
application: mysample
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
main.py
import webapp2
from google.cloud import storage
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, World!')
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
appengine_config.py
from google.appengine.ext import vendor
import os
# Third-party libraries are stored in "lib", vendoring will make
# sure that they are importable by the application.
if os.path.isdir(os.path.join(os.getcwd(), 'lib')):
vendor.add('lib')
要求.txt
google-cloud
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)