参考一堆资料去折腾Flask开发
Quickstart — Flask-SQLAlchemy Documentation (2.1)
去Python的交互式环境中,执行导入app没问题:
(SIPEvents) ➜ SIPEvents python Python 2.7.12 (default, Aug 15 2016, 11:09:04) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from sipevents import db <div–<—————————————————————————— DEBUG in __init__ [sipevents/__init__.py:14]: db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’> <div–<—————————————————————————— |
但是再去导入models中的User失败:
>>> from sipevents import User Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: cannot import name User |
对应的代码是:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/models.py
from sipevents import app from . import db # from app import db class User(db.Model): __tablename__ = ‘wechat_users’ # Columns openid = db.Column(db.String(64), primary_key=True) |
对于代码:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/__init__.py
#!/usr/bin/python # -*- coding: UTF-8 -*- from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__, instance_relative_config=True) app.config.from_object(‘config’) # load config.py app.config.from_pyfile(‘config.py’) app.debug = app.config["FLASK_DEBUG"] db = SQLAlchemy(app) app.logger.debug("db=%s", db) from sipevents import views, models |
也去试了试其它的的,但是都不行:
>>> from sipevents import models >>> from sipevents import models.User File "<stdin>", line 1 from sipevents import models.User ^ SyntaxError: invalid syntax >>> from models import User Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named models >>> import models.User Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named models.User |
Flask SQLAlchemy ImportError: cannot import name User
python – ImportError when importing from a lower module – Stack Overflow
Python, Flask, SQLAlchemy: cannot import from models – Stack Overflow
Flask: circular import problem
后来去:
/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/__init__.py
中,多加了导入User:
#!/usr/bin/python # -*- coding: UTF-8 -*- from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__, instance_relative_config=True) app.config.from_object(‘config’) # load config.py app.config.from_pyfile(‘config.py’) app.debug = app.config["FLASK_DEBUG"] db = SQLAlchemy(app) app.logger.debug("db=%s", db) from sipevents import views, models from models import User |
就可以了:
(SIPEvents) ➜ SIPEvents python Python 2.7.12 (default, Aug 15 2016, 11:09:04) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from sipevents import db <div–<—————————————————————————— DEBUG in __init__ [sipevents/__init__.py:14]: db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’> <div–<—————————————————————————— >>> from sipevents imo^Hp File "<stdin>", line 1 from sipevents imp ^ SyntaxError: invalid syntax >>> from sipevents import User >>> crifanUserJson = { … "province": u"江苏", … "openid": "oswjmv4X0cCXcfkIwjoDfCkeTVVY", … "headimgurl": "http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0", … "language": u"zh_CN", … "city": u"苏州", … "country": u"中国", … "sex": 1, … "privilege": [], … "nickname": u"礼貌" … } >>> app.logger.debug(‘crifanUserJson=%s’, crifanUserJson) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name ‘app’ is not defined >>> from sipevents import app >>> app.logger.debug(‘crifanUserJson=%s’, crifanUserJson) <div–<—————————————————————————— DEBUG in <stdin> [<stdin>:1]: crifanUserJson={‘province’: u’\u6c5f\u82cf’, ‘openid’: ‘oswjmv4X0cCXcfkIwjoDfCkeTVVY’, ‘headimgurl’: ‘http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib7GeE5OFrWiaRqsK1ruW1HmGaziaYETV5vQhIIbic6wHKFQ/0’, ‘language’: u’zh_CN’, ‘city’: u’\u82cf\u5dde’, ‘privilege’: [], ‘country’: u’\u4e2d\u56fd’, ‘nickname’: u’\u793c\u8c8c’, ‘sex’: 1} <div–<—————————————————————————— >>> >>> crifanUser = User(openid = crifanUserJson[‘openid’], … province = crifanUserJson[‘province’], … headimgurl = crifanUserJson[‘headimgurl’], … language = crifanUserJson[‘language’], … city = crifanUserJson[‘city’], … country = crifanUserJson[‘country’], … sex = crifanUserJson[‘sex’], … nickname = crifanUserJson[‘nickname’]) >>> app.logger.debug(‘crifanUser=%s’, crifanUser) <div–<—————————————————————————— DEBUG in <stdin> [<stdin>:1]: crifanUser=<User u’\u793c\u8c8c’ oswjmv4X0cCXcfkIwjoDfCkeTVVY> <div–<—————————————————————————— >>> db.create_all() >>> db.session.add(crifanUser) >>> db.session.commit() >>> currentUsers = User.query.all() >>> app.logger.debug(‘currentUsers=%s’, currentUsers) <div–<—————————————————————————— DEBUG in <stdin> [<stdin>:1]: currentUsers=[<User u’\u793c\u8c8c’ oswjmv4X0cCXcfkIwjoDfCkeTVVY>] <div–<—————————————————————————— >>> |
转载请注明:在路上 » [已解决]Flask开发期间在python交互命令行中导入User失败:ImportError cannot import name User