获取 ModuleNotFoundError:没有名为“MySQLdb”的模块

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

我创建了一个安装了以下内容的 virtualenv:

$ pip freeze
aniso8601==9.0.1
attrs==22.2.0
click==8.1.3
configparser==5.3.0
Flask==2.2.2
flask-restplus==0.13.0
Flask-SQLAlchemy==3.0.2
greenlet==2.0.1
itsdangerous==2.1.2
Jinja2==3.1.2
jsonschema==4.17.3
MarkupSafe==2.1.1
mysql-connector-python==8.0.31
protobuf==3.20.1
PyMySQL==1.0.2
pyrsistent==0.19.2
pytz==2022.7
six==1.16.0
SQLAlchemy==1.4.45
Werkzeug==2.2.2

您可以看到我安装了 PyMySQL 和 mysql-connector-python,但我仍然收到此错误:

Traceback (most recent call last):
  File "/home/ciasto/Dev/crud_app/src/start.py", line 1, in <module>
    from todo_app.extensions import db
  File "/home/ciasto/Dev/crud_app/src/todo_app/extensions.py", line 5, in <module>
    db = SQLAlchemy(app)
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 219, in __init__
    self.init_app(app)
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 320, in init_app
    engines[key] = self._make_engine(key, options, app)
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/flask_sqlalchemy/extension.py", line 606, in _make_engine
    return sa.engine_from_config(options, prefix="")
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 743, in engine_from_config
    return create_engine(url, **options)
  File "<string>", line 2, in create_engine
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned
    return fn(*args, **kwargs)
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/engine/create.py", line 548, in create_engine
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/ciasto/Dev/crud_app/crud_app_env/lib/python3.10/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 163, in dbapi
    return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'

我已尝试重新安装 pip 安装 MySQL-python pip 安装 ConfigParser pip 安装 MySQLdb

我已经尝试了ModuleNotFoundError:没有名为“MySQLdb”的模块

的解决方案

但这对我不起作用。 我在 x86 平台上使用 ubuntu jammy。

python mysql sqlalchemy mysql-python
2个回答
0
投票

我能够通过首先安装来解决这个问题

apt install libmysqlclient-dev

然后

pip install mysqlclient


0
投票

安装mysql连接器包

pip install mysql-connector-python

编辑您的数据库 URL

SQLALCHEMY_URL = "mysql+mysqlconnector://your_username:your_password@localhost/your_database"
© www.soinside.com 2019 - 2024. All rights reserved.