我目前正在使用flask和AppEngine技术开发python服务器。当我意识到我需要DB时,我尝试使用 sqlite3 ,但是有一些麻烦:
INFO 2016-07-03 12:25:35,324 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2016-07-03 12:25:35,950 sdk_update_checker.py:257] The SDK is up to date.
INFO 2016-07-03 12:25:36,029 api_server.py:205] Starting API server at: http://localhost:55540
INFO 2016-07-03 12:25:36,032 dispatcher.py:197] Starting module "default" running at: http://127.0.0.1:8080
INFO 2016-07-03 12:25:36,035 admin_server.py:116] Starting admin server at: http://localhost:8000
ERROR 2016-07-03 12:25:39,164 wsgi.py:263]
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/macbook/Documents/HSE/python/analyticsserver-1359/main.py", line 6, in <module>
import sqlite3
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
from dbapi2 import *
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sqlite3/dbapi2.py", line 28, in <module>
from _sqlite3 import *
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 963, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named _sqlite3
INFO 2016-07-03 12:25:39,177 module.py:788] default: "GET / HTTP/1.1" 500 -
这是我的 main.py
# import the Flask class from the flask module
from functools import wraps
from flask import Flask, render_template, redirect, url_for, request, session, flash, g
import sqlite3
# config
# create the application object
app = Flask(__name__)
app.database = 'sample.db'
app.secret_key = 'my precious'
# login required decorator
def login_required(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('You need to login first.')
return redirect(url_for('login'))
return wrap
@app.route('/')
def home():
return redirect(url_for('welcome'))
@app.route('/sql')
def sql():
g.db = connect_db()
cur = g.db.execute('SELECT * FROM posts')
posts = [dict(title=row[0], description=row[1]) for row in cur.fetchall()]
g.db.close()
return render_template('index.html', posts=posts) # render a template
# use decorators to link the function to a url
@app.route('/welcome')
def welcome():
return render_template('welcome.html') # render a template
@app.route('/main')
def mainpage():
return render_template('main.html') # render a template
# route for handling the login page logic
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if (request.form['username'] != 'admin') \
or request.form['password'] != 'admin':
error = 'Invalid Credentials. Please try again.'
else:
session['logged_in'] = True
flash('You were logged in.')
return redirect(url_for('home'))
return render_template('login.html', error=error)
@app.route('/logout')
@login_required
def logout():
session.pop('logged_in', None)
flash('You were logged out.')
return redirect(url_for('welcome'))
# connect to database
def connect_db():
return sqlite3.connect(app.database)
if __name__ == '__main__':
app.run()
我一直试图在谷歌和这里找到一些东西,但我无法意识到出了什么问题
UPD:我使用的是Mac OS X和PyCharm
答案 0 :(得分:1)
开发环境非常难以模拟部署环境。部分尝试意味着阻止导入在部署环境中不可用的内容(如sqlite3
)。
您可以针对本地MySQL
实例进行开发。见https://cloud.google.com/appengine/docs/python/cloud-sql/#testing_and_deploying