我正在ImportError: No module named 'mysql'
执行以下操作...
>>> import mysql.connector
MySQL 已安装并且在 Python 3.5 上。我想不通。上述命令在 Python 2.7 中运行良好。
我正在ImportError: No module named 'mysql'
执行以下操作...
>>> import mysql.connector
MySQL 已安装并且在 Python 3.5 上。我想不通。上述命令在 Python 2.7 中运行良好。
For me (OS X 10.11.4, Python 3.5.1), the mysql-connector-package installed itself (by default) into Python 2.7 that was also on my system. I copied the mysql package directory from:
/Library/Python/2.7/site-packages/mysql
to /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mysql
. Everything works. According to http://dev.mysql.com/doc/connector-python/en/connector-python-versions.html the connector package supports Python 3.3+.
Your specific installation path for the module may be different, but you can easily check this from the REPL using the sys.path: https://leemendelowitz.github.io/blog/how-does-python-find-packages.html
不幸的是,没有可用于 python3.5的 mysql-connector 。
因此,您可以使用pymysql模块来替代mysql-connector 包
pip install pymysql
import pymysql
Connection = pymysql.connect(host='hostname', user='username', password='password',
db='database',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor )
ExecuteQuery(Connection)
Connection.close()
由于我们需要将 MySQl 与 Python 连接起来,因此 Python 命令假定连接器已启用。所以我们需要遵循以下步骤:
import mysql.connector
.它应该运行,在我的情况下它确实运行了!
从无法运行 MySql 实用程序:
MYSQL Utilities 假定已安装 MySQL Connector for Python。如果你安装它(http://dev.mysql.com/downloads/connector/python/),MySQL Utilities 应该可以正常运行。
我认为此链接可以帮助您解决问题。