neutrino
neutrino

Reputation: 924

No module named 'MySQLdb' , but cannot pip install ( and its not a problem with pip )

I am getting an error when i try to POST some data with my flask app.

here is my sqlalchemy class with 3 fields, and the corresponding schema class :

class Plant(db.Model):     
  id = db.Column(db.Integer, primary_key=True)     
  name = db.Column(db.String(100))     
  dur = db.Column(db.Integer)          

  def __init__(self, name, dur):         
    self.name = name         
    self.dur = dur  

class PlantSchema(ma.SQLAlchemySchema):     
  class Meta:         
    model = Plant 

Here is the POST request in app.py, in order to add a plant model to my database :

plant_schema = PlantSchema()  

@app.route('/plants', methods=['POST']) 
def add_plant():     
  name = request.json.get('name', '')     
  dur = request.json.get('dur', '')     
  plant = Plant(name=name, dur=dur)     
  db.session.add(plant)     
  db.session.commit()     
  return plant_schema.jsonify(plant) 

unfortunately, when i POST my request in Postman, i get the following

<head>
    <title>ModuleNotFoundError: No module named 'MySQLdb'
        </title>...

When i try to pip install mysqlclient ( which is the recommended fix ), i get :

Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      /bin/sh: mysql_config: command not found
      /bin/sh: mariadb_config: command not found
      /bin/sh: mysql_config: command not found
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/j5/b194wjbn32sbqct7crpvcp2w0000gn/T/pip-install-ijejonzz/mysqlclient_841539cf1eed4511a135f8e95cb2be75/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/private/var/folders/j5/b194wjbn32sbqct7crpvcp2w0000gn/T/pip-install-ijejonzz/mysqlclient_841539cf1eed4511a135f8e95cb2be75/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/private/var/folders/j5/b194wjbn32sbqct7crpvcp2w0000gn/T/pip-install-ijejonzz/mysqlclient_841539cf1eed4511a135f8e95cb2be75/setup_posix.py", line 31, in mysql_config
          raise OSError("{} not found".format(_mysql_config_path))
      OSError: mysql_config not found
      mysql_config --version
      mariadb_config --version
      mysql_config --libs
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

if the problem is not with pip, im not sure what else to try?

Upvotes: 0

Views: 2270

Answers (1)

eandersson
eandersson

Reputation: 26342

You need to install the MySQL Client development libraries. The installer is unable to find the required executable mysql_config on your system.

Ubuntu

sudo apt install libmysqlclient-dev

CentOS

sudo yum install mariadb-devel

Upvotes: 1

Related Questions