我正在尝试为sqlalchemy数据库创建Restful端点。但由于某种原因,它无法找到Flask Restless扩展。
当我运行该文件时,我收到此错误:
Traceback (most recent call last):
File "./rest1.py", line 7, in <module>
import flask.ext.restless
File "/home/ian/git_dev/flask_rest/test2/flask/local/lib/python2.7/site-packages/flask/exthook.py", line 86, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named flask.ext.restless
我安装了Flask,Flask-Restless和SQLalchemy,但它仍然无效。
Django==1.6.5
Flask==0.10.1
Flask-Restless==0.13.1
Jinja2==2.7.2
MarkupSafe==0.23
PAM==0.4.2
Pillow==2.3.0
SQLAlchemy==0.9.4
这是我的代码:
#!flask/bin/python
#!/usr/bin/python
# -*- mode: python -*-
from flask import Flask
from sqlalchemy import Column, Date, DateTime, Float, Integer, Unicode
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
import flask.ext.restless
app = Flask(__name__)
engine = create_engine('sqlite:////tmp/testdb.sqlite', convert_unicode=True)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
mysession = scoped_session(Session)
Base = declarative_base()
Base.metadata.bind = engine
class Test_Table(Base):
__tablename__ = 'plot'
id = Column(DateTime, primary_key=True)
value = Column(Integer)
type = Column(Unicode)
max = Column(Integer)
min = Column(Integer)
Base.metadata.create_all()
manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
manager.create_api(Test_Table, methods=['GET', 'POST', 'PUT' 'DELETE'])
app.run()
我在虚拟环境内外都尝试过Flask-Restful。但我仍然得到同样的结果。
答案 0 :(得分:2)
您可以尝试重新安装pip install --upgrade --force-reinstall flask_restless
如果不起作用:
从github安装flask_restless 0.14-dev
。
git clone https://github.com/jfinkels/flask-restless
cd flask-restless
pip install -r requirements-doc.txt
使用import flask_restless
flask_restless.__version__
'0.13.1'
确保python正在查看flask_restless.
你的道路上似乎也有一个“烧瓶”目录:
'/home/ian/git_dev/flask_rest/test2/flask/local/lib/python2.7/site-packages/flask/exthook.py
'
可能与您的导入产生冲突。
答案 1 :(得分:1)
您可以直接从Github安装最新的flask-restless:
pip install git+https://github.com/jfinkels/flask-restless.git
像往常一样,在安装之前,如果您正在使用像virtualenv这样的东西,请确保您处于正确的环境中