简体   繁体   English

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

[英]ModuleNotFoundError: No module named 'MySQLdb'

After finishing of one of my Flask projects, I uploaded it on github just like everybody else.在完成我的一个 Flask 项目后,我和其他人一样将它上传到了 github。 after a 2-3 months period I downloaded the entire githube repository on another machine to run it. 2-3 个月后,我在另一台机器上下载了整个 githube 存储库来运行它。 However, the app is not working because the packages are not found giving the following message但是,该应用程序无法运行,因为未找到提供以下消息的软件包

ModuleNotFoundError: No module named 'Flask' ModuleNotFoundError: 没有名为“Flask”的模块

So I ended up downloading all packages starting from Flask, SQLalchemy,..etc!所以我最终下载了从 Flask、SQLalchemy 等开始的所有软件包! but I got stuck with MySQLdb :但我被MySQLdb卡住了:

(MYAPPENV) C:\Users\hp\myapp>python run.py
Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from app import app
  File "C:\Users\hp\myapp\app\__init__.py", line 4, in <module>
    from instance.config import engine
  File "C:\Users\hp\myapp\instance\config.py", line 52, in <module>
    engine = create_engine("mysql://root:root@localhost/MYAPPDB")
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\__init__.py", line 425, in create_engine
return strategy.create(*args, **kwargs)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\engine\strategies.py", line 81, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
  File "C:\Users\hp\AppData\Local\Programs\Python\Python37-32\lib\site-packages\sqlalchemy\dialects\mysql\mysqldb.py", line 102, in dbapi
    return __import__('MySQLdb')
ModuleNotFoundError: No module named 'MySQLdb'

Could anybody please help with this issue?有人可以帮忙解决这个问题吗? I am using python37 on windows machine.我在 Windows 机器上使用 python37。 I even tried downloading packages such as mysqlclient,..etc but it didn't work out.我什至尝试下载诸如 mysqlclient,..etc 之类的软件包,但没有成功。

I have read that mysqldb is not supported by python3我已经读过 python3 不支持 mysqldb

And it looks like when you are trying to connect to your database you are using mysql db to connect to the database by default!看起来当您尝试连接到数据库时,默认情况下您正在使用 mysql db 连接到数据库!

you need to change it by editing your DATABASE_URI configuration您需要通过编辑您的 DATABASE_URI 配置来更改它

But before you need to install the connector extension :但在您需要安装连接器扩展之前:

with this command :使用此命令:

pip install mysql-connector-python

And according to this documentation you can edit your DATABASE_URI and change the default connector like this :根据 此文档,您可以编辑 DATABASE_URI 并更改默认连接器,如下所示:

DATABSE_URI='mysql+mysqlconnector://{user}:{password}@{server}/{database}'.format(user='your_user', password='password', server='localhost', database='dname')

I hope this will help...我希望这个能帮上忙...

You can install mysqlclient with pip您可以使用pip安装mysqlclient

If using Python3 , try this:如果使用Python3 ,试试这个:

pip3 install mysqlclient

or in Python2或在Python2 中

pip install mysqlclient

要安装MySQLdb ,只要您的机器上安装了pippip3

pip install mysqlclient
import pymysql
pymysql.install_as_MySQLdb()

I have the same problem like you.我和你有同样的问题。 Here is my solution:这是我的解决方案:

Reason: python3.X does not support MySQLdb,so you need to change to pymysql model原因: python3.X不支持MySQLdb,需要改成pymysql模型

Solution: Change the content of import.解决方法:更改导入的内容。

1): 1):

replace all MySQLdb with pymysql

2): 2):

def reconnect(self):
    """Closes the existing database connection and re-opens it."""
    self.close()
    self._db = pymysql.connect(**self._db_args)# MySQLdb.connect(**self._db_args)
    self._db.autocommit(True)

3) 3)

if pymysql is not None:
    # Fix the access conversions to properly recognize unicode/binary
    FIELD_TYPE = pymysql.connections.FIELD_TYPE # MySQLdb.constants.FIELD_TYPE
    FLAG = pymysql.constants.FLAG# MySQLdb.constants.FLAG
    CONVERSIONS = copy.copy (pymysql.converters.conversions)# (MySQLdb.converters.conversions)

    field_types = [FIELD_TYPE.BLOB, FIELD_TYPE.STRING, FIELD_TYPE.VAR_STRING]
    if 'VARCHAR' in vars(FIELD_TYPE):
        field_types.append(FIELD_TYPE.VARCHAR)

    for field_type in field_types:
        # CONVERSIONS[field_type] = [(FLAG.BINARY, str)] + CONVERSIONS[field_type]
        CONVERSIONS[field_type] = [(FLAG.BINARY, str)].append(CONVERSIONS[field_type])

    # Alias some common MySQL exceptions
    IntegrityError = pymysql.IntegrityError# MySQLdb.IntegrityError
    OperationalError = pymysql.OperationalError# MySQLdb.OperationalError

4): 4):

def __init__(self, host, database, user=None, password=None,
                 max_idle_time=7 * 3600, connect_timeout=10,# 设置连接超时时间,时间是秒
                 time_zone="+0:00", charset = "utf8", sql_mode="TRADITIONAL"):

5): 5):

def query(self, query, *parameters, **kwparameters):
    """Returns a row list for the given query and parameters."""
    cursor = self._cursor()
    try:
        self._execute(cursor, query, parameters, kwparameters)
        column_names = [d[0] for d in cursor.description]
        return [Row(itertools.zip_longest(column_names, row)) for row in cursor]
    finally:
        cursor.close()

Hello internet people,你好互联网人,

I reach this page because of the same problem.由于同样的问题,我到达此页面。
But I'm using Ubuntu 18.04 and Python 3.6但我使用的是 Ubuntu 18.04 和 Python 3.6

Python snippet - app.py Python 片段 - app.py

from flask import Flask, render_template
from flask_mysqldb import MySQL

app = Flask(__name__)

app.config['MYSQL_HOST'] = 'YourHost' 
app.config['MYSQL_USER'] = 'YourUserName'
app.config['MYSQL_PASSWORD'] = 'UserPassword'
app.config['MYSQL_DB'] = 'TheDbName'
mysql = MySQL(app)

app.secret_key = 'YourUltimateSuperSecretKey'

@app.route('/')
@app.route('/index')
def index():
    cur = mysql.connection.cursor()
    cur.execute('SELECT * FROM users')
    data = cur.fetchall()
    cur.close()
    return render_template('index.html', test = data)

if __name__ == "__main__":
    app.run(debug=True)

HTML - index.html HTML - index.html

<div>
    {% for x in test %}
        {{ x }}
    {% endfor %}
</div>

So this is what i did to solve the problem:所以这就是我为解决问题所做的:
First I installed the missing package:首先我安装了缺少的包:

sudo apt install python3-mysqldb sudo apt 安装 python3-mysqldb

After that I installed those libraries with pip on the environment: 之后,我在环境中使用 pip 安装了这些库:

pip install Flask-MySQLdb mysql-connector-python pip 安装 Flask-MySQLdb mysql-connector-python

and it worked :) 它奏效了:)

You should first install python3-dev, default-libmysqlclient-dev, build-essential then you can install mysqlclient.你应该先安装python3-dev、default-libmysqlclient-dev、build-essential然后你就可以安装mysqlclient了。

For Red Hat对于红帽

sudo yum install python3-devel mysql-devel
pip install mysqlclient

For Debian对于 Debian

sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
pip install mysqlclient

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlalchemy ModuleNotFoundError:没有名为“MySQLdb”的模块 - sqlalchemy ModuleNotFoundError: No module named 'MySQLdb' Scrapy ModuleNotFoundError:没有名为“MySQLdb”的模块 - Scrapy ModuleNotFoundError: No module named 'MySQLdb' ModuleNotFoundError:anaconda 中没有名为“MYSQLdb”的模块 - ModuleNotFoundError: No module named 'MYSQLdb' in anaconda 获取 ModuleNotFoundError:没有名为“MySQLdb”的模块 - Getting ModuleNotFoundError: No module named 'MySQLdb' ModuleNotFoundError:没有名为“flask-mysqldb”的模块 - ModuleNotFoundError: No module named 'flask-mysqldb' 在 GCP 上部署 OpenLiteSpeed django 解决方案时出现“ModuleNotFoundError(”No module named 'MySQLdb'“,)” - “ModuleNotFoundError(”No module named 'MySQLdb'“,)” when deploying a OpenLiteSpeed django solution on GCP ModuleNotFoundError:没有名为“MySQLdb”的模块 Amazon MySQL RDS SQLAlchemy - ModuleNotFoundError: No module named 'MySQLdb' Amazon MySQL RDS SQLAlchemy ModuleNotFoundError:没有名为“MySQLdb”的模块python macOS high sierra - ModuleNotFoundError: No module named 'MySQLdb' python macOS high sierra ModuleNotFoundError:即使已安装,也没有名为“flask_mysqldb”的模块 - ModuleNotFoundError: No module named 'flask_mysqldb' even if installed 没有名为MySQLdb的模块 - No module named MySQLdb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM