码迷,mamicode.com
首页 > 其他好文 > 详细

有的时候真的很坑 抄代码都能错

时间:2017-03-19 17:38:17      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:module   ***   open   code   current   log   script   py3   model   

别人用py2 写的教程,我用的是py3,我这尼玛,有多难知道吗..

import imp 过期了..我只能用google去国外找资料..

后来官方查到

 1 import imp
 2 from migrate.versioning import api
 3 from app import db
 4 from config import SQLALCHEMY_DATABASE_URI
 5 from config import SQLALCHEMY_MIGRATE_REPO
 6 migration = SQLALCHEMY_MIGRATE_REPO + /versions/%03d_migration.py % (api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1)
 7 tmp_module = imp.new_module(old_model)
 8 old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
 9 exec old_model in tmp_module.__dict__
10 script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata)
11 open(migration, "wt").write(script)
12 api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
13 print New migration saved as  + migration
14 print Current database version:  + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO))
15 
16 
17 ******************py3写法*************************
18 import importlib
19 from migrate.versioning import api
20 from app import db
21 from config import SQLALCHEMY_DATABASE_URI,SQLALCHEMY_MIGRATE_REPO
22 
23 migration = SQLALCHEMY_MIGRATE_REPO + /versions/%03d_migration.py % (
24 api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) + 1)
25 tmp_module = importlib.import_module(old_model)#
26 old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
27 exec (old_model in tmp_module.__dict__)
28 script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta,
29                                           db.metadata)
30 open(migration, "wt").write(script)
31 api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
32 print(New migration saved as  + migration)
33 print(Current database version:  + str(api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)))
34 
35 就能解决报错..哎.做个啥都不容易.抄都能出错.还得解决..

 

有的时候真的很坑 抄代码都能错

标签:module   ***   open   code   current   log   script   py3   model   

原文地址:http://www.cnblogs.com/Hbmask/p/6580832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!