Python3.5拒绝使用MySQL连接器

时间:2016-05-04 23:27:39

标签: python mysql-python mysql-connector python-3.5 mysql-connector-python

我在这里读了几个问题,人们和我的情况一样,但无论我尝试什么方法,它都行不通!

根据MySQL文档,MySQL-connector-python-2.x.x.x应该适用于Python 3+。我已经下载了RPM软件包并将其安装在我的virtualenv中。但是,在导入库时,我收到错误“没有名为MySQL的模块”。

检查我是否安装了包裹:

(virtual)[centos ~]$ rpm -ql mysql-connector-python-2.1.3-1.el6.x86_64.rpm
package mysql-connector-python-2.1.3-1.el6.x86_64.rpm is not installed

尝试安装软件包:

(virtual)[centos ~]$ sudo rpm -i ~/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm 
[sudo] password for <username>: 
warning: /home/<username>/Documents/python/mysql-connector-python-2.1.3-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
    package mysql-connector-python-2.1.3-1.el6.x86_64 is already installed

这适用于Python2.6:

[centos ~]$ python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> import mysql.connector
>>> 

不在Python3.5上:

[centos ~]$ python3.5
Python 3.5.1 (default, Jan 13 2016, 17:43:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>> 

通过MySQL文档验证了包:

(virtual)[centos]$ python
Python 3.5.1 (default, Jan 13 2016, 17:43:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>> 
>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'mysql'
>>> 

确保python3.5与MySQL.connector在同一目录中

(virtual)[centos]$ which python3.5
~/Documents/redhat-server/python_project/virtual/bin/python3.5

更新

(virtual)[centos]$ pip install mysql-connector-python
Collecting mysql-connector-python
  Could not find a version that satisfies the requirement mysql-connector-python (from versions: )
No matching distribution found for mysql-connector-python
(virtual)[centos]$

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我已经弄明白了。 Anzel说它在全局安装在默认的python2.6目录中是正确的。以下是我修复它的方法:

启动默认的python解释器,在我的情况下&#34; python&#34; - &GT; python2.6,并运行下面的命令,看看&#34; MySQL.connector&#34;已安装。

>>> from distutils.sysconfig import get_python_lib
>>> print get_python_lib ()
/usr/lib/python2.6/site-packages
>>> 

导航到上面的目录,&#34; /usr/lib/python2.6/site-packages /&#34;显示一个名为MySQL的目录,以及一个名为MySQL_connector_python-2.1.3-py2.6.egg-info的文件。

启动python3.5解释器引导我走向另一条路径,我检查过,当然,两个MySQL和MySQL_connector_python-2.1.3-py2.6.egg-info文件/目录都没有。< / p>

>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>> 

接下来,我将这两个文件复制到Python3.5 site-packages目录,测试了导入,数据库连接,并且工作正常!

Python 3.5.1 (default, Jan 13 2016, 17:43:34) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "copyright", "credits" or "license()" for more information.
>>> 
>>> import mysql.connector
>>> 
>>> cnx = mysql.connector.connect (user = 'username', password = 'password', host = 'localhost',
               database = 'testdb')
>>> cnx
<mysql.connector.connection.MySQLConnection object at 0x7ffa4c1009b0>