连接mysql的问题

问题描述 投票:0回答:1

我正在尝试在 Mac (Python3) 上的 IDLE 上运行简单的 Python 代码,但是

回溯(最近一次调用最后一次):

文件“/Users/.../Documents/connDB.py”,第 1 行,位于 导入 mysql.connector ModuleNotFoundError:没有名为“mysql”的模块

(base) lib % which mysql
/usr/local/bin/mysql

(base)  lib % which python
/Users/username/anaconda3/bin/python

lib % which python3
/Users/username/anaconda3/bin/python3

 mysql -u root -p         
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.32 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

那么是什么阻止了 Python 连接到我的数据库?

mysql python-3.x mysql-python
1个回答
0
投票

您的Python环境中未安装mysql.connector模块。 您需要安装 mysql-connector-python 包才能使用 mysql.connector。您可以使用 pip(Python 的包管理器)来安装它。 打开终端并运行以下命令:

pip install mysql-connector-python

确保您使用的是要运行脚本的 Python 环境。安装该包后,您应该能够在 Python 脚本中导入 mysql.connector,不会出现任何问题。

如果您使用 Anaconda,您可能需要使用 Anaconda 的包管理器 conda 安装包,而不是 pip:

conda install -c anaconda mysql-connector-python

这确保了与 Anaconda 环境的兼容性。安装后,您应该能够在 Python 脚本中导入 mysql.connector 并连接到您的 MySQL 数据库。

© www.soinside.com 2019 - 2024. All rights reserved.