Flask中,去使用Flask-SQLAlchemy创建数据库
结果运行出错:
>>> from app import db <div–<—————————————————————————— DEBUG in app [app.py:35]: db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/sipevents.db’> <div–<—————————————————————————— >>> db.create_all() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 972, in create_all self._execute_for_all_tables(app, bind, ‘create_all’) File "/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 964, in _execute_for_all_tables op(bind=self.get_engine(app, bind), **extra) File "/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 910, in get_engine return connector.get_engine() File "/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 545, in get_engine self._engine = rv = sqlalchemy.create_engine(info, **options) File "/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 386, in create_engine return strategy.create(*args, **kwargs) File "/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 75, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 339, in dbapi raise e ImportError: No module named pysqlite2 >>> |
搜:
ImportError: No module named pysqlite2
ImportError: No module named ‘pysqlite2’ – 简书
ImportError: No module named pysqlite2 – Carl – 博客频道 – CSDN.NET
ImportError: No module named ‘pysqlite2’ · Issue #464 · jupyterhub/jupyterhub
此处的:
/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py
中是:
@classmethod def dbapi(cls): try: from pysqlite2 import dbapi2 as sqlite except ImportError as e: try: from sqlite3 import dbapi2 as sqlite # try 2.5+ stdlib name. except ImportError: raise e return sqlite |
继续参考:
ImportError: No module named ‘pysqlite2’ – 简书
去搜索并安装:
sqlite-devel
(SIPEvents) ➜ SIPEvents yum search sqlite Loaded plugins: security base | 3.7 kB 00:00 http://mirror.centos.org/centos/6/sclo/x86_64/rh/repodata/repomd.xml: [Errno 12] Timeout on http://mirror.centos.org/centos/6/sclo/x86_64/rh/repodata/repomd.xml: (28, ‘Connection time-out’) Trying other mirror. elrepo | 2.9 kB 00:00 epel | 4.3 kB 00:00 epel/primary_db | 5.9 MB 00:05 extras | 3.4 kB 00:00 updates | 3.4 kB 00:00 updates/primary_db | 1.4 MB 00:01 ============================================================= N/S Matched: sqlite ============================================================= apr-util-sqlite.x86_64 : APR utility library SQLite DBD driver asterisk-sqlite.x86_64 : Sqlite modules for Asterisk 。。。 Running Transaction Installing : sqlite-devel-3.6.20-1.el6_7.2.x86_64 1/1 Verifying : sqlite-devel-3.6.20-1.el6_7.2.x86_64 1/1 Installed: sqlite-devel.x86_64 0:3.6.20-1.el6_7.2 Complete! |
然后再去运行试试
结果问题依旧:
难道是:
刚才安装的:
sqlite-devel
是针对于Python 3才适用的?
难道此处的Python 2.7是需要:
sqlite2-devel
???
去安装试试:
(SIPEvents) ➜ SIPEvents yum install -y sqlite2-devel Loaded plugins: security Setting up Install Process Resolving Dependencies <div–<> Running transaction check <div–<-> Package sqlite2-devel.x86_64 0:2.8.17-10.el6 will be installed <div–<> Processing Dependency: sqlite2(x86-64) = 2.8.17-10.el6 for package: sqlite2-devel-2.8.17-10.el6.x86_64 <div–<> Processing Dependency: libsqlite.so.0()(64bit) for package: sqlite2-devel-2.8.17-10.el6.x86_64 <div–<> Running transaction check <div–<-> Package sqlite2.x86_64 0:2.8.17-10.el6 will be installed <div–<> Finished Dependency Resolution Dependencies Resolved =============================================================================================================================================== Package Arch Version Repository Size =============================================================================================================================================== Installing: sqlite2-devel x86_64 2.8.17-10.el6 epel 155 k Installing for dependencies: sqlite2 x86_64 2.8.17-10.el6 epel 158 k Transaction Summary =============================================================================================================================================== Install 2 Package(s) Total download size: 314 k Installed size: 1.0 M Downloading Packages: (1/2): sqlite2-2.8.17-10.el6.x86_64.rpm | 158 kB 00:00 (2/2): sqlite2-devel-2.8.17-10.el6.x86_64.rpm | 155 kB 00:00 <div–<——————————————————————————————————————————————— Total 1.3 MB/s | 314 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : sqlite2-2.8.17-10.el6.x86_64 1/2 Installing : sqlite2-devel-2.8.17-10.el6.x86_64 2/2 Verifying : sqlite2-devel-2.8.17-10.el6.x86_64 1/2 Verifying : sqlite2-2.8.17-10.el6.x86_64 2/2 Installed: sqlite2-devel.x86_64 0:2.8.17-10.el6 Dependency Installed: sqlite2.x86_64 0:2.8.17-10.el6 Complete! |
然后再去运行代码试试
结果问题依旧:
File "/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 339, in dbapi raise e ImportError: No module named pysqlite2 |
python – ImportError: No Module Named ‘pysqlite2’ – Stack Overflow
python – No module named _sqlite3 – Stack Overflow
好像是:
即使安装了sqlite-devel,也需要:
重新(编译)安装Python的?
python mysql SQLAlchemy ImportError: No module named pysqlite2-好折腾-生命在于好折腾
>>> import sqlite3 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/sqlite3/__init__.py", line 24, in <module> from dbapi2 import * File "/usr/local/lib/python2.7/sqlite3/dbapi2.py", line 28, in <module> from _sqlite3 import * ImportError: No module named _sqlite3 >>> import sqlite Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named sqlite |
那就去:
然后再去试试:
终于可以了:
(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 app import db <div–<—————————————————————————— DEBUG in app [app.py:35]: db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/sipevents.db’> <div–<—————————————————————————— >>> db.create_all() >>> |
[总结]
此处,
python2.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py
中,报错:
ImportError: No module named pysqlite2
的原因是:
当前CentOS系统中缺少对应的sqlite
注:
此处,关于sqlite:
之前叫做:
pysqlite2
后来sqlite已经合并到Python的标准库中,改名叫做:
sqlite3
了。
所以对应的
sqlalchemy/dialects/sqlite/pysqlite.py
中也已经做了对应的处理了:
@classmethod def dbapi(cls): try: from pysqlite2 import dbapi2 as sqlite except ImportError as e: try: from sqlite3 import dbapi2 as sqlite # try 2.5+ stdlib name. except ImportError: raise e return sqlite |
所以,此处出错,不是由于pysqlite2没改为sqlite3而导致出错的
而是:
当前(CentOS中所安装的Python 2.7)系统中,的确没有包含对应的sqlite库而导致报错的。
解决办法:
(1)去当前CentOS系统中,先去安装sqlite
yum install -y sqlite-devel |
注:
1.此处的sqlite-devel,应该就是对应的sqlite3的devel
而另外的:
sqlite2-devel,应该就是指的是:sqlite2的devel
2.如果是Debian/Ubuntu等系统,是用:
apt-get install libsqlite3-dev |
(2)再去重新安装Python
-》否则Python中还是找不到对应的sqlite库的
基本的逻辑是:
用Python 2.7的源码,去configure和make,再make install
(再去接着安装setuptools和pip)
详见:
转载请注明:在路上 » [已解决]初始化Flask-SQLAlchemy数据库出错:ImportError No module named pysqlite2