ModuleNotFoundError:没有名为"MySQLdb"的模块Amazon MySQL RDS SQLAlchemy [英] ModuleNotFoundError: No module named 'MySQLdb' Amazon MySQL RDS SQLAlchemy
问题描述
我在将Amazon AWS MySQL与SQLAlchemy连接时遇到问题.根据指示,我已连接.
I am having issues with connecting Amazon AWS MySQL with SQLAlchemy. According to the instruction, I have connected.
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://<user>:<password>@<host>/<dbname>
但是有一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__
return type.query_class(mapper, session=self.sa.session())
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/orm/scoping.py", line 74, in __call__
return self.registry()
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/util/_collections.py", line 1001, in __call__
return self.registry.setdefault(key, self.createfunc())
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/orm/session.py", line 2950, in __call__
return self.class_(**local_kw)
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 143, in __init__
bind = options.pop('bind', None) or db.engine
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 877, in engine
return self.get_engine()
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 896, in get_engine
return connector.get_engine()
File "/home/ec2-user/venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 559, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/engine/__init__.py", line 424, in create_engine
return strategy.create(*args, **kwargs)
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 81, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/home/ec2-user/venv/lib/python3.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 102, in dbapi
return __import__('MySQLdb')
ModuleNotFoundError: No module named 'MySQLdb'
我正在使用Python3.7版本.即使根据stackoverflow,我已经安装了pymysql,但仍然面临问题.
I am using Python3.7 version. Even according to the stackoverflow, I have installed pymysql, but still facing the problem.
谢谢您的答复.
推荐答案
步骤1 如果要使用MySQLDB,则需要使用以下命令之一.哪一个取决于您拥有和使用的操作系统和软件.
Step 1 If you want to use MySQLDB you need to use one of the following commands. Which one depends on what OS and software you have and use.
easy_install mysql-python
(混合OS)
pip install mysql-python
(mix os/python 2)
pip install mysql-python
(mix os/ python 2)
pip install mysqlclient
(mix os/python 3)
pip install mysqlclient
(mix os/ python 3)
apt-get install python-mysqldb
(Linux Ubuntu,...)
apt-get install python-mysqldb
(Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean
(FreeBSD)
yum install MySQL-python
(Linux Fedora,CentOS ...)
yum install MySQL-python
(Linux Fedora, CentOS ...)
For Windows, see this answer: Install mysql-python (Windows)
第2步:
- 创建引擎
engine = create_engine('mysql+mysqldb://...', pool_recycle=3600)
使用create_engine.pool_recycle
选项,以确保如果连接在池中存在固定的秒数,则该连接将被丢弃并替换为新的连接:
use the create_engine.pool_recycle
option which ensures that a connection will be discarded and replaced with a new one if it has been present in the pool for a fixed number of seconds:
- 创建连接对象
conn = engine.connect()
- 执行SQL查询
conn.execute("SELECT * From table;")
这篇关于ModuleNotFoundError:没有名为"MySQLdb"的模块Amazon MySQL RDS SQLAlchemy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!