1

我正在ImportError: No module named 'mysql'执行以下操作...

>>> import mysql.connector

MySQL 已安装并且在 Python 3.5 上。我想不通。上述命令在 Python 2.7 中运行良好。

4

4 回答 4

2

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

于 2016-04-29T19:23:47.110 回答
2

不幸的是,没有可用于 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()
于 2016-03-14T07:48:26.687 回答
1

由于我们需要将 MySQl 与 Python 连接起来,因此 Python 命令假定连接器已启用。所以我们需要遵循以下步骤:

  1. 打开 MySQL 安装程序;
  2. 转到“自定义设置”;
  3. 转到“连接器”;
  4. 根据您使用的选择连接器\Python 和版本;
  5. 添加它;
  6. 使用import mysql.connector.

它应该运行,在我的情况下它确实运行了!

于 2018-09-07T11:48:44.057 回答
0

从无法运行 MySql 实用程序:

MYSQL Utilities 假定已安装 MySQL Connector for Python。如果你安装它(http://dev.mysql.com/downloads/connector/python/),MySQL Utilities 应该可以正常运行。

我认为此链接可以帮助您解决问题。

于 2015-11-18T10:00:10.643 回答