2016-07-03 55 views
0

我目前正在開發使用瓶和AppEngine技術的python服務器。當我意識到我需要DB,我試圖用sqlite3的,但也有一些麻煩:ImportError:沒有名爲_sqlite3的模塊python 2.7

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

您不能使用GAE運行任意數據庫,並且您當然不能運行sqlite,因爲這取決於對文件系統的訪問。你爲什麼要這樣做?該文檔將告訴您如何使用內置數據存儲。 –

+0

@DanielRoseman thanx! – Andrey

回答

相關問題