无法导入 Flask-Restless

Posted

技术标签:

【中文标题】无法导入 Flask-Restless【英文标题】:Unable to import Flask-Restless 【发布时间】:2014-03-06 10:35:33 【问题描述】:

所以我正在尝试使用 Flask-Restless。不幸的是,我无法导入 flask.ext.restless。

>>> from flask.ext.restless import APIManager
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module
    __import__(realname)
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/__init__.py", line 22, in <module>
    from .manager import APIManager
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/manager.py", line 17, in <module>
    from .views import API
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/views.py", line 114
    except ProcessingException, exception:

>>> import flask.ext.restless
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask/exthook.py", line 62, in load_module
    __import__(realname)
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/__init__.py", line 22, in <module>
    from .manager import APIManager
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/manager.py", line 17, in <module>
    from .views import API
  File "/Users/mrkaiser/.virtualenvs/dcenv/lib/python3.3/site-packages/flask_restless/views.py", line 114
    except ProcessingException, exception:
                              ^
SyntaxError: invalid syntax

这些是我当前 virtualenv 中的包。

$ pip freeze       
Flask==0.10.1
Flask-Admin==1.0.7
Flask-Restless==0.12.1
Flask-SQLAlchemy==1.0
Jinja2==2.7.2
MarkupSafe==0.18
SQLAlchemy==0.9.2
WTForms==1.0.5
Werkzeug==0.9.4
itsdangerous==0.23
mysql-connector-python==1.1.5
python-dateutil==2.2
six==1.5.2

我一直在尝试做这个教程(虽然略有修改): 作者 = 'mrkaiser'

import flask
from sqlalchemy import Column, Integer, String, Sequence
from sqlalchemy import ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship
from sqlalchemy.orm import scoped_session, sessionmaker
from flask.ext.restless import APIManager

app = Flask(__name__)
engine = create_engine('mysql+mysqlconnector://flask:dummypass@localhost/test', convert_unicode=True)
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
mysession = scoped_session(Session)

Base = declarative_base()
Base.metadata.bind = engine


class Person(Base):
    __tablename__ = 'people'

    id = Column(Integer, Sequence('person_id_seq'), primary_key=True)
    first_name = Column(String(50))
    last_name = Column(String(50))
    url = Column(String(150))
    about = Column(String(400))

    def __repr__(self):
    return "Person<first_name=%s,last_name=%s,url=%s" % (self.    first_name, self.last_name, self.url)


# Create the database tables.
Base.metadata.create_all()

# Create the Flask-Restless API manager.
manager = flask.ext.restless.APIManager(app, session=mysession)

# Create API endpoints, which will be available at /api/<tablename> by
# default. Allowed HTTP methods can be specified as well.
manager.create_api(Person, methods=['GET'])


# start the flask loop
app.run()

当我通过命令行运行它时,我得到了同样的错误。不知道该怎么办。谢谢!

【问题讨论】:

根据 Flask-Restless 文档,python 3(我正在使用的)似乎在 0.13(在 dev 分支上)之前不受支持。 在这里遇到同样的问题 【参考方案1】:

对于那些在 python 3 中遇到同样问题的烧瓶 restless,升级到current dev version。问题已修复 here。

pip install git+https://github.com/jfinkels/flask-restless/

【讨论】:

以上是关于无法导入 Flask-Restless的主要内容,如果未能解决你的问题,请参考以下文章

带有mod_wsgi的flask-restless无法连接到MySQL服务器

ImportError:无法导入名称而无法导入类

mysql 表导入向导无法导入 csv 文件

导入 theano 时出错“无法导入名称 gof”

无法从'@angular/forms'导入“导入FormGroup,FormControl [重复]

vulfocus无法导入镜像