import mysql.connector 报错
Posted 渔夫数据库笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了import mysql.connector 报错相关的知识,希望对你有一定的参考价值。
1.操作系统版本
uname -a
Linux 10-27-30-48 5.15.60-1.el7.x86_64 #1 SMP Thu Aug 11 12:39:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
2.python 版本
python 3.5
3.问题现象
安装完python 3.5 之后,创建隔离环境并安装 mysql-connector-python,但是在导入该模块的时候报错
1)检查是否安装了mysql-connector-python
source /root/venv_python/bin/activate
(venv_python) [root@ecs-5gcmp-hadoop-0003 site-packages]# pip list
et-xmlfile (1.1.0)
mysql-connector-python (8.0.31)
openpyxl (3.0.10)
pip (8.1.1)
protobuf (3.20.1)
pycrypto (2.6.1)
setuptools (20.10.1)
xlwt (1.3.0)
##隔离环境中安装了 8.0.31 版本的 mysql-connector-python (当前最新版本为8.0.32)
2) 导入 mysql.connector 报错
python
Python 3.5.2 (default, Oct 25 2022, 16:14:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import xlwt
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3.5/lib/python3.5/site-packages/mysql/connector/__init__.py", line 32, in <module>
from .connection_cext import CMySQLConnection
File "/usr/local/python3.5/lib/python3.5/site-packages/mysql/connector/connection_cext.py", line 67
f"MySQL Connector/Python C Extension not available (exc)"
^
SyntaxError: invalid syntax
3)怀疑是版本问题,更换较低版本
pip install mysql-connector-python==8.0.21
Collecting mysql-connector-python==8.0.21
Downloading https://files.pythonhosted.org/packages/f9/69/b998b6e461d189bf504c81e1a2df6a131ce75b4e15bf6655de62df801443/mysql_connector_python-8.0.21-cp35-cp35m-manylinux1_x86_64.whl (15.8MB)
100% |████████████████████████████████| 15.8MB 70kB/s
Requirement already satisfied (use --upgrade to upgrade): protobuf>=3.0.0 in ./venv_python/lib/python3.5/site-packages (from mysql-connector-python==8.0.21)
Installing collected packages: mysql-connector-python
Found existing installation: mysql-connector-python 8.0.31
Uninstalling mysql-connector-python-8.0.31:
Successfully uninstalled mysql-connector-python-8.0.31
Successfully installed mysql-connector-python-8.0.21
## mysql-connector-python 版本列表请见 https://dev.mysql.com/doc/relnotes/connector-python/en/
4)再次导入成功
venv_python) [root@10-27-30-48 ~]# pip list
mysql-connector-python (8.0.21)
pip (8.1.1)
protobuf (3.20.1)
setuptools (20.10.1)
You are using pip version 8.1.1, however version 22.3 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv_python) [root@10-27-30-48 ~]# python
Python 3.5.2 (default, Oct 25 2022, 21:28:13)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
以上是关于import mysql.connector 报错的主要内容,如果未能解决你的问题,请参考以下文章
import mysql.connector ModuleNotFoundError: No module named 'mysql.connector'; 'mysql' 不是一个包
pycharm编写python,图中import MySQLdb没问题,import mysql.connector出错,为啥会出错?
MySQL Connector/J 6.x jdbc.properties 配置, mysql-connector-java-6.0.4.jar 异常