37

I created virtualenv for django 1.9 project. I am trying to pip install mysqlclient or mysql-python but both of them gives me errors.

pip install mysqlclient

pip install mysql-python

both give me the same error message:

Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/r4/bkv_4t9s4r140pjkgv6lsq8w0000gn/T/pip-build-cdxcssp9/mysqlclient

any suggestions!?

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
hongjooy
  • 443
  • 1
  • 5
  • 7

7 Answers7

80

Try to run this before:

Ubuntu:

sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install pymysql
pip install mysqlclient

In OSX:

sudo xcodebuild -license accept
brew install mysql-connector-c
JayRizzo
  • 3,234
  • 3
  • 33
  • 49
arcegk
  • 1,480
  • 12
  • 15
  • the first two commands give me, Reading Package Lists... Done Building Dependency Tree... Done E: Couldn't find package python-dev Reading Package Lists... Done Building Dependency Tree... Done E: Couldn't find package libmysqlclient-dev.. – hongjooy Feb 04 '16 at 09:52
  • 3
    So, run `brew install mysql-connector-c` – arcegk Feb 04 '16 at 15:49
  • any suggestions on how to do it for centos 7? – badiya Feb 13 '19 at 13:35
  • @arcegk this should be an answer! only this really helped me. – Imaskar May 02 '19 at 07:53
  • Here is my solution: https://saugatbhattarai.com.np/error-while-installing-mysqlclient-in-virtualenv/ – Saugat Bhattarai Aug 28 '21 at 12:21
  • The macOS one really helped me. It works now! Just make sure after you install the second one with brew you set up the environment variables how it suggests there – Ax M Jul 06 '22 at 13:47
5

This is probably due to your mysql_config being broken.

As of 2019, here is how to run smoothly pip install mysqlclient on MacOS:

brew info openssl and follow the commands at the bottom

  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc
  export LDFLAGS="-L/usr/local/opt/openssl/lib"
  export CPPFLAGS="-I/usr/local/opt/openssl/include"

Other approaches:

  • brew install/upgrade/reinstall mysql : did not fix the issue for me, but has the nice side effect to make sure your installation is clean.
  • brew install mysql-connector-c : to make that work you have to unlink mysql, which ruins your setup and it did not fix the issue for me.
louis_guitton
  • 5,105
  • 1
  • 31
  • 33
  • Hi, are these steps valid? I'm having tough time setting up a django project with mysql, absolutely nothing is working for me – Dev1ce Sep 08 '19 at 08:50
  • Is your brew up to date ? What the error you're having ?? – louis_guitton Sep 09 '19 at 19:44
  • I had the error `ld: library not found for -lssl` in MacOS 10.15. This answer worked for me, but I had to append both `export LDFLAGS` and `export CPPFLAGS` in `~/.bashrc` file, to export the variables permanently – manuel_b Apr 15 '20 at 21:31
2

If you're using Anaconda (which I highly suggest you) then run these two commands

conda install -c anaconda mysql-connector-python

and

conda install -c bioconda mysqlclient
Akash Desarda
  • 704
  • 8
  • 6
1
  1. Download the MySQL APT repository config tool (you can see more details here: http://dev.mysql.com/downloads/repo/apt/)

    wget http://dev.mysql.com/get/mysql-apt-config_0.7.3-1_all.deb
    
  2. Install the MySQL APT repository config tool

    dpkg -i mysql-apt-config_0.7.3-1_all.deb
    

You will be asked to select product and version that you want to install. In the first step, select Server and next select either mysql-5.6 or mysql-5.7. Then click Apply.

  1. Update APT

    apt-get update
    
  2. Install the server

    sudo apt-get install mysql-community-server
    
    sudo apt-get install python-dev python3-dev
    sudo apt-get install libmysqlclient-dev
    pip install pymysql
    pip install mysqlclient
    
Charles
  • 1,121
  • 19
  • 30
hkjamil
  • 100
  • 1
  • 8
1

If you are using Python 2.x, and already have installed:

  • MySQL Server
  • Python Connector

Then the problem is when you run:

Windows:

(your environment) SomePath> pip install mysqlclient 

Mac OS:

$ pip install mysqlclient

Actually it is asking to install package for Python 3.x not 2.x. So it is throwing an error.

Solution is to run:

Windows:

(your environment) SomePath> pip install mysqlclient==1.3.9

Mac OS:

$ pip install mysqlclient==1.3.9

P.S The mysqlclient==1.3.9 version is the latest version for Python 2.x

cezar
  • 11,616
  • 6
  • 48
  • 84
karjaubayev
  • 571
  • 5
  • 10
0

For Ubuntu 18.04

sudo apt-get install python-dev python3-dev
sudo apt-get install libmysqlclient-dev
pip install pymysql
pip install mysqlclient

and setup

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

0

This worked for on Ubuntu 18.04

sudo apt-get install python3.6-dev 
sudo apt-get install mysql-client 
sudo apt-get install libsqlclient-dev 
sudo apt-get install libssl-dev
Anurag Reddy
  • 1,159
  • 11
  • 19