最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]SQLAlchemy中初始化mysql出错:ImportError: No module named MySQLdb

MySQL crifan 6031浏览 0评论

折腾:

[未解决]SQLAlchemy中把sqlite换成mysql

期间,

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/sipevents/instance/config.py

# mysql://username:password@hostname/database
SQLALCHEMY_DATABASE_URI = ‘mysql://root:Jiandao123@localhost/sipevents’

然后去初始化,测试mysql,结果出错:

Traceback (most recent call last):
  File “db_create.py”, line 34, in <module>
    db.drop_all()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py”, line 980, in drop_all
    self._execute_for_all_tables(app, bind, ‘drop_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 387, in create_engine
    return strategy.create(*args, **kwargs)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py”, line 80, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py”, line 92, in dbapi
    return __import__(‘MySQLdb’)
ImportError: No module named MySQLdb

SQLAlchemy ImportError: No module named MySQLdb

2012: Flask 环境中 ImportError: No module named MySQLdb 问题_白糖_新浪博客

creating engine in python, “No module named ‘MySQLdb’ – Google Groups

Import Error : No module named mysql – Ask OpenStack: Q&A Site for OpenStack Users and Developers

python – ImportError: No module named MySQLdb – Stack Overflow

python – No module named MySQLdb – Stack Overflow

(SIPEvents) ➜  SIPEvents yum install MySQL-python
Loaded plugins: security
Setting up Install Process
Resolving Dependencies

<div–<> Running transaction check

<div–<-> Package MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 will be installed

<div–<> Finished Dependency Resolution

Dependencies Resolved
=======================================================================================================================================================================================
 Package                                       Arch                                    Version                                             Repository                             Size
=======================================================================================================================================================================================
Installing:
 MySQL-python                                  x86_64                                  1.2.3-0.3.c1.1.el6                                  base                                   86 k
Transaction Summary
=======================================================================================================================================================================================
Install       1 Package(s)
Total download size: 86 k
Installed size: 246 k
Is this ok [y/N]: y
Downloading Packages:
MySQL-python-1.2.3-0.3.c1.1.el6.x86_64.rpm                                                                                                                      |  86 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64                                                                                                                              1/1 
  Verifying  : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64                                                                                                                              1/1 
Installed:
  MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6                                                                                                                                             
Complete!
(SIPEvents) ➜  SI

结果错误依旧。

(SIPEvents) ➜  SIPEvents pip install pymysql     
Collecting pymysql
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f4890aa8a10>: Failed to establish a new connection: [Errno -2] Name or service not known’,)’: /simple/pymysql/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f4890aa8bd0>: Failed to establish a new connection: [Errno -2] Name or service not known’,)’: /simple/pymysql/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f4890aa8c90>: Failed to establish a new connection: [Errno -2] Name or service not known’,)’: /simple/pymysql/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f4890aa8e50>: Failed to establish a new connection: [Errno -2] Name or service not known’,)’: /simple/pymysql/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None)) after connection broken by ‘NewConnectionError(‘<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f4890aa8fd0>: Failed to establish a new connection: [Errno -2] Name or service not known’,)’: /simple/pymysql/
  Could not find a version that satisfies the requirement pymysql (from versions: )
No matching distribution found for pymysql
(SIPEvents) ➜  SIPEvents
pip install mysql-python
  —————————————-
  Failed building wheel for mysql-python
  Running setup.py clean for mysql-python
Failed to build mysql-python
Installing collected packages: mysql-python
  Running setup.py install for mysql-python … error
    Complete output from command /root/Envs/SIPEvents/bin/python -u -c “import setuptools, tokenize;__file__=’/tmp/pip-build-cIjtRU/mysql-python/setup.py’;exec(compile(getattr(tokenize, ‘open’, open)(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))” install –record /tmp/pip-xwSPC0-record/install-record.txt –single-version-externally-managed –compile –install-headers /root/Envs/SIPEvents/include/site/python2.7/mysql-python:
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-2.7
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-2.7
    creating build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-2.7/MySQLdb
    creating build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-2.7/MySQLdb/constants
    running build_ext
    building ‘_mysql’ extension
    creating build/temp.linux-x86_64-2.7
    gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,5,’final’,1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector –param=ssp-buffer-size=4 -m64 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -fno-strict-aliasing -fwrapv -fPIC -DUNIV_LINUX -DUNIV_LINUX
    _mysql.c:44:23: error: my_config.h: No such file or directory
    _mysql.c:46:19: error: mysql.h: No such file or directory
    _mysql.c:2642: error: (near initialization for ‘_mysql_ResultObject_memberlist[0].offset’)
    _mysql.c: In function ‘_mysql_ConnectionObject_getattr’:
    _mysql.c:2666: error: ‘_mysql_ConnectionObject’ has no member named ‘open’
    error: command ‘gcc’ failed with exit status 1
    —————————————-
Command “/root/Envs/SIPEvents/bin/python -u -c “import setuptools, tokenize;__file__=’/tmp/pip-build-cIjtRU/mysql-python/setup.py’;exec(compile(getattr(tokenize, ‘open’, open)(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))” install –record /tmp/pip-xwSPC0-record/install-record.txt –single-version-externally-managed –compile –install-headers /root/Envs/SIPEvents/include/site/python2.7/mysql-python” failed with error code 1 in /tmp/pip-build-cIjtRU/mysql-python/
 pip install mysqlclient
    _mysql.c: In function ‘_mysql_ConnectionObject_getattro’:
    _mysql.c:2680: error: ‘_mysql_ConnectionObject’ has no member named ‘open’
    error: command ‘gcc’ failed with exit status 1
    —————————————-
Command “/root/Envs/SIPEvents/bin/python -u -c “import setuptools, tokenize;__file__=’/tmp/pip-build-wYy50N/mysqlclient/setup.py’;exec(compile(getattr(tokenize, ‘open’, open)(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))” install –record /tmp/pip-zHcUvq-record/install-record.txt –single-version-externally-managed –compile –install-headers /root/Envs/SIPEvents/include/site/python2.7/mysqlclient” failed with error code 1 in /tmp/pip-build-wYy50N/mysqlclient/
(SIPEvents) ➜  SIPEvents                       
(SIPEvents) ➜  SIPEvents yum install mysql-devel.x86_64
Loaded plugins: security
Setting up Install Process
Resolving Dependencies

<div–<> Running transaction check

<div–<-> Package mysql-devel.x86_64 0:5.1.73-7.el6 will be installed

<div–<> Finished Dependency Resolution

Dependencies Resolved
=======================================================================================================================================================================================
 Package                                       Arch                                     Version                                           Repository                              Size
=======================================================================================================================================================================================
Installing:
 mysql-devel                                   x86_64                                   5.1.73-7.el6                                      base                                   130 k
Transaction Summary
=======================================================================================================================================================================================
Install       1 Package(s)
Total download size: 130 k
Installed size: 388 k
Is this ok [y/N]: y
Downloading Packages:
mysql-devel-5.1.73-7.el6.x86_64.rpm                                                                                                                             | 130 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : mysql-devel-5.1.73-7.el6.x86_64                                                                                                                                     1/1 
  Verifying  : mysql-devel-5.1.73-7.el6.x86_64                                                                                                                                     1/1 
Installed:
  mysql-devel.x86_64 0:5.1.73-7.el6                                                                                                                                                    
Complete!

No module named MySQLdb when trying to load the OpenStack dashboard – Ask Ubuntu

ImportError: No module named MySQLdb – 雾中世界 – 博客园

Ubuntu14.04下解决No module named MySQLdb错误 – 太午的作业本 – 博客频道 – CSDN.NET

ImportError No module named MySQLdb解决方法 | 大爱

搜:

MySQL for Python

MySQL-python 1.2.5 : Python Package Index

(SIPEvents) ➜  SIPEvents pip install MySQL-python
Collecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
Building wheels for collected packages: MySQL-python
  Running setup.py bdist_wheel for MySQL-python … done
  Stored in directory: /root/.cache/pip/wheels/38/a3/89/ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Successfully built MySQL-python
Installing collected packages: MySQL-python
Successfully installed MySQL-python-1.2.5

然后就可以了。

[总结]

此处主要是:

pip install MySQL-python

主页是:

Index of Packages : Python Package Index

注意,不是:

pip install mysql-python

就可以解决:

ImportError: No module named MySQLdb

的问题了。

【后记】

实际上,此处:pip中,库的名字好像是不分大小写的

-》后来遇到同样问题,就是通过安装mysql-python:

(RunningFast) ➜  RunningFast pip install mysql-python
Collecting mysql-python
Installing collected packages: mysql-python
Successfully installed mysql-python-1.2.5

而解决问题的。

转载请注明:在路上 » [已解决]SQLAlchemy中初始化mysql出错:ImportError: No module named MySQLdb

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.199 seconds, using 22.20MB memory