簡體   English   中英

無法在GAE應用中使用google-cloud

[英]Unable to use google-cloud in a GAE app

我的Google App Engine應用程序( webapp.py )中的以下行無法導入Google Cloud庫:

from google.cloud import storage

出現以下錯誤:

ImportError: No module named google.cloud.storage

我做了一些研究,發現以下文章很有幫助:

結合上述文章提出的技術,我做了以下工作:

  1. 創建requirements.txt文件:

     google-cloud==0.19.0 
  2. 使用pip導入此庫:

     pip install -t lib -r requirements.txt 
  3. 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雲存儲寫入/讀取的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

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')

requirements.txt

google-cloud

App Engine SDK附帶了一個App Engine特定的Google雲端存儲API ,可用於處理雲存儲存儲桶。

import cloudstorage as gcs

您是否有理由不使用此內置庫,無需加載配置?

你的appengine_config.py只需要包含:

from google.appengine.ext import vendor
vendor.add('lib')

你發布的所有其他內容對我來說都很好看。

軟件包名稱空間似乎已在此github問題中指出已更改,並且尚未完全修復。 您可以安裝使用不同命名空間的舊版本( pip install gcloud )並使用此import語句:

from gcloud import storage

您還應該確保在appengine_config.py中導入供應商庫,如magicray的答案中所指出的那樣。

這個問題似乎已經解決,因為google-cloud版本為0.20.0。 所以問題中的import語句應該有效。 只記得運行pip install --upgrade google-cloud

你應該將“import google”行移到sys.path.insert之后的位置

我遇到了同樣的問題,並花了很長時間來解決它。

設置1 :如果您的app.yaml設置如下:

runtime: python27
api_version: 1
threadsafe: no

handlers:
- url: /.*
  script: main.app

libraries:
- name: jinja2
  version: "2.6"
- name: markupsafe
  version: "0.15

並且您的main.py您的腳本注冊為應用:

from flask import Flask
app = Flask(__name__)
app.config['DEBUG'] = True

@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    return 'Hello World!'

你應該可以在谷歌雲上運行得很好。

設置2 :我如何遇到ImportModule error -
在我的app.yaml文件中,我用script=main.app替換了script=main.app並刪除了

from flask import Flask
app = Flask(__name__)
app.config['DEBUG'] = True

來自main.py file 因此,不會運行appengine_config.py來設置sys路徑。 解決方案是您可以按照設置1之類的模式,或者將以下內容添加到main.py文件中。

from google.appengine.ext import vendor
vendor.add('lib')

然后,應用程序應具有導入第三方軟件包的正確路徑。 希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM