✓ Solved

Mysqlclient won't install in Virtual Environment.

I just downloaded your excellent book Understanding databases. I created my directories and installed my virtual environment. I am now working on Section 6 page 10 installing requirements into the venv. I pip installed the following items with no problems.

django>=3.2,<4.0 : This gives us the latest long-term support version of Django (known as the most
recent LTS)
• gunicorn : our production-grade web server
• python-dotenv : for loading environment variables in Django
• black (optional): for auto-formatting code
• pillow : also known as the Python Image Library mainly for the ImageField in Django
• boto3 : for using Linode Object Storage with Python
• django-storages for using Linode Object Storage with Django (depends on boto3 )

When I got to:

• mysqlclient : for connecting Django to a production-grade MySQL database.

I got this error:
Running setup.py install for mysqlclient did not run successfully.
MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory

     46 | #include "Python.h"
        |          ^~~~~~~~~~
  compilation terminated.
  error: command '/usr/bin/x86_64-linux-gnu-gcc' failed with exit code 1
  [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> mysqlclient

In searching the internet it talks about installing mysqldb. But that seems strange since we are trying to install this code into a container and we want the db in a separate container.

I am working on a solution, but I was wondering if anyone has already solved this issue.

2 Replies

✓ Best Answer

I found a solution by searching "pip won't install mysqlclient"

https://askubuntu.com/questions/1321141/unable-to-install-mysqlclient-on-ubuntu-20-10

After installing the suggested packages I was able to pip install mysqlclient.

The strange thing is that I used apt to install the missing packages, and yet my mysqlclient installation was into the virtual environment I had created for this exercise.

So there may be some hoops to jump through when building the container. We may have to add the same files to the container.

The failure is that a C header file (Python.h) that is required by some module gcc is trying to compile is missing.

There's some other mysql glue required by one or more of your python modules that you need to install. I have no idea what that might be but I'm pretty confident that's your problem.

Did you install the mysql development package (mysql-dev) with apt (or whatever package manager you're using) first? This package consists of libraries & header files required to compile other programs/libraries (like your python glue) that use mysql.

-- sw

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct