我的 Google App Engine 应用程序 (webapp.py
) 中的以下行无法导入 Google Cloud图书馆:
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://stackoverflow.com/a/34585485
- https://www.simonmweber.com/2013/06/18/python-protobuf-on-app-engine.html
结合使用上述文章建议的技术,我执行了以下操作:
创建一个
requirements.txt
文件:google-cloud==0.19.0
使用
pip
导入这个库:pip install -t lib -r requirements.txt
在我的
appengine_config.py
文件中使用以下代码: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
文件中。
检查这个简单示例是否适合您(您应该不会遇到任何导入错误)。
创建以下文件并运行 pip install -r requirements.txt -t lib
。我的网站上不需要任何其他东西就可以正常工作。
app.yaml
application: mysample
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
主.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')
requirements.txt
google-cloud
关于python - 无法在 GAE 应用程序中使用 google-cloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39704367/