![](/img/trans.png)
[英]Google App Engine Launcher (Python) - ImportError: No module named webapp2
[英]Python Error - ImportError: no module named 'webapp2'
我是Python的初學者,在我的計算機上本地運行python googleppengine代碼時遇到很大的困難。
我的代碼如下所示:
import json
import urllib
import os
import webapp2
from google.appengine.ext.webapp import template
import datetime
from google.appengine.ext import db
class Events(db.Model):
venue_name = db.StringProperty()
address = db.StringProperty()
id = db.StringProperty()
venue_id = db.StringProperty()
# hire_date = db.DateProperty()
# attended_hr_training = db.BooleanProperty()
class eventSearch(webapp2.RequestHandler):
def get(self):
base_url = 'http://api.eventful.com/json/events/search?app_key=zGtDX6cwQjCRdkf6&l=dublin&?q=music'
response = urllib.urlopen(base_url)
html = response.read()
html = json.loads(html)
result = html['venues']
result1 = result['venue']
當我在cmd提示符中使用命令“ python file.py”運行此代碼時,收到以下錯誤:
Traceback <most recent call last>:
File "file.py", line 4, in <module>
import webapp2
ImportError: No module named 'webapp2'
我已經1.按照如何在Windows中添加到pythonpath中的建議創建了一個PythonPath 。 在帶有目錄的系統變量中:C:\\ Python33 \\ DLLs; C:\\ Python33 \\ Lib; C:\\ Python33 \\ Lib \\ lib2to3; C:\\ Program Files(x86)\\ Google \\ google_appengine; C:\\ Program Files(x86 )\\ Google \\ google_appengine \\ lib;
然后,我還將以下兩個目錄也都添加到了我的“ PATH”變量中,如答案中所建議的那樣- 即使我未安裝webapp2,導入webapp2仍可在google-app-engine上使用
C:\\ Program Files(x86)\\ Google \\ google_appengine \\; C:\\ Program Files(x86)\\ Google \\ google_appengine \\ lib
編輯:在提供的答案中提出了建議之后,我也意識到GAE不支持Python的3.3版,我試圖在問題的前一部分中運行它。 卸載Python33並安裝Python27后,更改系統變量以反映新的Python27后,我仍然遇到問題,並且我的代碼無法使用GAE啟動器上傳。 我在我的日志控制台(GAE啟動器)中收到以下錯誤:
2013-04-14 22:59:19 Running command: "['C:\\Python27\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8001', 'C:\\Users\\Karen\\Desktop\\Development\\projects\\file']"
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 193, in <module>
_run_file(__file__, globals())
File "C:\Program Files (x86)\Google\google_appengine\dev_appserver.py", line 189, in _run_file
execfile(script_path, globals_)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\devappserver2\devappserver2.py", line 30, in <module>
from google.appengine.datastore import datastore_stub_util
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_stub_util.py", line 45, in <module>
from google.appengine.api import api_base_pb
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\api_base_pb.py", line 20, in <module>
from google.net.proto import ProtocolBuffer
File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 22, in <module>
import httplib
File "C:\Python27\lib\httplib.py", line 71, in <module>
import socket
File "C:\Python27\lib\socket.py", line 47, in <module>
import _socket
ImportError: Module use of python25.dll conflicts with this version of Python.
2013-04-14 22:59:21 (Process exited with code 1)
感謝您能為我提供的任何幫助。
您不應該安裝webapp2。 它包含在SDK中,並且已經在生產運行時中。
閱讀有關配置作為Appengine環境一部分的庫的信息https://developers.google.com/appengine/docs/python/python25/migrate27#Configuring_Libraries
這是包含的第三方庫的列表。
https://developers.google.com/appengine/docs/python/tools/libraries27
如果您將pip / easy_install用於其他各種庫,那么您會發現這本身是不夠的。 您將需要在項目中鏈接或包括這些庫,對sys.path進行操作以便找到它們,並確保已部署這些庫。
這解決了我的問題(您遇到了同樣的問題)
首先不要嘗試從python IDLE運行從Google App引擎運行打開localhost:port
如果仍然顯示相同的錯誤,請在GAE中打開日志,請嘗試以下步驟
看起來appengine / tools / devappserver2 / python / sandbox.py應該可以將C:\\path\\to\\google_appengine\\google
轉換為C:\\path\\to\\google_appengine
,但是還有一個額外的dirname,所以最終得到了C:\\path\\to
我不確定為什么它只會在某些情況下引起問題。
您可以通過以下方法解決此問題:
library_pattern = os.path.join(os.path.dirname(
os.path.dirname(google.__file__)), _THIRD_PARTY_LIBRARY_FORMAT_STRING)
至:
library_pattern = os.path.join(
os.path.dirname(google.__file__), _THIRD_PARTY_LIBRARY_FORMAT_STRING)
通過在應用引擎代碼中的各個位置放置raise Exception(sys.path)
並重新啟動開發服務器,我發現了此問題。
您可以使用pip或easy_install安裝webapp2。 請參閱http://webapp-improved.appspot.com/tutorials/quickstart.nogae.html快速了解
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.