簡體   English   中英

ImportError:沒有名為_sqlite3 python 2.7的模塊

[英]ImportError: No module named _sqlite3 python 2.7

我目前正在使用flask和AppEngine技術開發python服務器。 當我意識到我需要數據庫時,我嘗試使用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()

我一直在嘗試在Google和此處查找內容,但我不知道出了什么問題

UPD:我正在使用Mac OS X和PyCharm

開發環境非常努力地模擬部署環境。 這種嘗試的一部分意味着防止導入部署環境中不可用的東西(例如sqlite3 )。

您可以針對本地MySQL實例進行開發。 請參閱https://cloud.google.com/appengine/docs/python/cloud-sql/#testing_and_deploying

暫無
暫無

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

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