18

I installed django 1.8.5 in virtualenv and using python 3.4.3 the worked displayed the it works page when using sqlite

I wanted to use mysql and I'm trying to install mysqlclient using

pip install mysqlclient

and I'm getting the following message

----------------------------------------
Failed building wheel for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient
Complete output from command /home/sasidhar/django/env/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-5lj39q67/mysqlclient/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-da2_35zs-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/sasidhar/django/env/include/site/python3.4/mysqlclient:
running install
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.4/MySQLdb
running build_ext
building '_mysql' extension
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,3,6,'final',1) -D__version__=1.3.6 -I/usr/include/mysql -I/usr/include/python3.4m -I/home/sasidhar/django/env/include/python3.4m -c _mysql.c -o build/temp.linux-x86_64-3.4/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g -DNDEBUG
_mysql.c:40:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------
Command "/home/sasidhar/django/env/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip-build-5lj39q67/mysqlclient/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-da2_35zs-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/sasidhar/django/env/include/site/python3.4/mysqlclient" failed with error code 1 in /tmp/pip-build-5lj39q67/mysqlclient

I did try installing libraries suggested in error: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

and still the problem persists please help me solve this problem
Thanks guys!!

Community
  • 1
  • 1
Sasidhar Boddeti
  • 1,214
  • 1
  • 11
  • 22

5 Answers5

38

You need to install python-dev:

sudo apt-get install python-dev

And, since you are using python3:

sudo apt-get install python3-dev

This command should help you.

If you are using mac os you might try:

brew update && brew rm python3 && brew install python3

You need to brew has been installed already, otherwise you can install it. It is very useful for getting packages in Mac OS. http://brew.sh/

Paul
  • 6,641
  • 8
  • 41
  • 56
30

For Ubuntu you also need to install build-essential

sudo apt-get install build-essential
Hrafn Malmquist
  • 432
  • 4
  • 10
  • 2
    this should be added to the main answer. it might be ubuntu specific (i don't know but i didn't need to install this on linux mint to solve the same problem) but it will help many people with this issue for sure. – nnov Nov 13 '19 at 12:44
  • 1
    I don't know if it needs to be answered to the main answer or not but it was definitely necessary in my case (Ubuntu 18.04) - +1 – Matt Cremeens Jan 13 '20 at 18:05
  • 1
    this really helps me, have tried installing python3-dev and other dependencies but then found the issues from this one – Gerry Jan 16 '20 at 19:06
  • this was the step that got it finally working for me (on ubuntu 20.04) – Karina Klinkevičiūtė Oct 04 '21 at 16:57
10

For those using different versions, such as Python 3.5, 3.6 or 3.7 I found this:

Part I

For Python 2.x use:

  $ sudo apt-get install python-dev

For Python 2.7 use:

  $ sudo apt-get install libffi-dev

For Python 3.x use:

  $ sudo apt-get install python3-dev

For Python 3.4 use:

  $ sudo apt-get install python3.4-dev

For Python 3.7 use:

  $ sudo apt-get install python3.7-dev

Part II

If this still doesn't help, others have pointed out to install

  $ sudo apt-get install build-essential

and finally

  $ sudo apt-get install libssl-dev

But the last two didn't help me personally. Just in case, hope it helps you.

david
  • 2,529
  • 1
  • 34
  • 50
5

For those using python3.5:

apt-get install python3.5-dev
wget https://bootstrap.pypa.io/get-pip.py
python3.5 get-pip.py
pip3.5 install mysqlclient
Psddp
  • 998
  • 10
  • 17
1

Tried all of the above suggestions and a few found elsewhere and nothing worked. In the end the solution was simple enough (courtesy of ignacionf on GitHub):

pip install git+https://github.com/PyMySQL/mysqlclient-python.git