Python3.5 拒绝使用 MySQL 连接器
Python3.5 Refuse to Work with MySQL Connector
我在这里看到了一些问题,有人和我有同样的情况,但不管我用什么方法都行不通!
根据 MySQL 文档,MySQL-connector-python-2.x.x.x 应与 Python 3+ 一起使用。我已经下载了 RPM 包并将其安装在我的 virtualenv 中。但是,在导入库时,出现错误 "No module named 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]$
我做错了什么?
我已经弄明白了。 Anzel 是正确的,它被全局安装在默认的 python2.6 目录中。这是我修复它的方法:
启动默认的 python 解释器,在我的例子中是 "python" -> python2.6,然后 运行 下面的命令查看 "MySQL.connector" 在哪里已安装。
>>> from distutils.sysconfig import get_python_lib
>>> print get_python_lib ()
/usr/lib/python2.6/site-packages
>>>
导航到上面的目录,“/usr/lib/python2.6/site-packages/”显示一个名为 MySQL 的目录和一个名为 MySQL_connector_python-2.1.3-py2 的文件。 6.egg-信息。
启动 python3.5 解释器让我走上了一条不同的道路,我检查了一下,果然,两个 MySQL 和 MySQL_connector_python-2.1.3-py2.6.egg -info file/directory 不存在。
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>>
接下来,我将这 2 个文件复制到 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>
我在这里看到了一些问题,有人和我有同样的情况,但不管我用什么方法都行不通!
根据 MySQL 文档,MySQL-connector-python-2.x.x.x 应与 Python 3+ 一起使用。我已经下载了 RPM 包并将其安装在我的 virtualenv 中。但是,在导入库时,出现错误 "No module named 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]$
我做错了什么?
我已经弄明白了。 Anzel 是正确的,它被全局安装在默认的 python2.6 目录中。这是我修复它的方法:
启动默认的 python 解释器,在我的例子中是 "python" -> python2.6,然后 运行 下面的命令查看 "MySQL.connector" 在哪里已安装。
>>> from distutils.sysconfig import get_python_lib
>>> print get_python_lib ()
/usr/lib/python2.6/site-packages
>>>
导航到上面的目录,“/usr/lib/python2.6/site-packages/”显示一个名为 MySQL 的目录和一个名为 MySQL_connector_python-2.1.3-py2 的文件。 6.egg-信息。
启动 python3.5 解释器让我走上了一条不同的道路,我检查了一下,果然,两个 MySQL 和 MySQL_connector_python-2.1.3-py2.6.egg -info file/directory 不存在。
>>> from distutils.sysconfig import get_python_lib
>>> print (get_python_lib ())
/home/<username>/Documents/redhat-server/python_project/virtual/lib/python3.5/site-packages
>>>
接下来,我将这 2 个文件复制到 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>