SaltyCrane Blog — Notes on JavaScript and web development

How to install MySQLdb in a virtualenv on Ubuntu Karmic

  • Install dependencies
    sudo apt-get build-dep python-mysqldb
  • Install MySQLdb with pip/virtualenv
    cd ~/lib/python-environments
    virtualenv --distribute --no-site-packages default
    pip install -E default/ MySQL-python

EnvironmentError: mysql_config not found

The first time, I did not install the dependencies and got the following error:

$ pip install -E default/ http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz/download
Downloading/unpacking http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz/download
  Downloading download (89Kb): 89Kb downloaded
  Running setup.py egg_info for package from http://sourceforge.net/projects/mysql-python/files/mysql-python-test/1.2.3c1/MySQL-python-1.2.3c1.tar.gz/download
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "", line 14, in 
      File "/tmp/pip-ZR_ZwM-build/setup.py", line 15, in 
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):
  
  File "", line 14, in 
  
  File "/tmp/pip-ZR_ZwM-build/setup.py", line 15, in 

    metadata, options = get_config()
  
  File "setup_posix.py", line 43, in get_config

    libs = mysql_config("libs_r")
  
  File "setup_posix.py", line 24, in mysql_config
    
    raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

----------------------------------------
Command python setup.py egg_info failed with error code 1
Storing complete log in ./pip-log.txt

Comments


#1 Alvin Mites commented on :

Thank you - was running into that same error using easy_install

pip install -E worked like a charm


#2 Israel commented on :

What is the cause of this happening?

i recently upgraded mysql with ubuntus upgrade manager, and it seems to have broken python-mysqldb

i tried your steps above but it did not work for me.


#3 Jason Peddle commented on :

How many times does this blog save my day. Answer: all of them. All of the times.


#4 semperos commented on :

Thank you. Just starting out, and I've depended on two of your posts in one day to get going. Great work.


#5 Oskar Sjödin commented on :

Thanks this post and the other covering virtualenv helped me out a lot.


#6 Thibault commented on :

I ran into the same problem, and lost a few minutes, because the python-dev package was not installed. For the record.


#7 Gene commented on :

Thanks for the sharing!!!! I suspect it was the build-dep part I was missing. My dog thanks you too, because I had to fix this before I could throw the ball for him.


#8 John Chase commented on :

Thank you; I had the same error without build-dep.


#9 robertjd commented on :

I believe it would be better to add the path to mysql_config to your PATH environment variable. This way you don't have to modify any files in the downloaded MySQL-python package.

I was using pip on OSX 10.7 and it could not find mysql_config, which is in /usr/local/mysql/bin/ (if you installed MySQL using the DMG installer) so I added this to PATH in my bin/activate script:

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
PATH="$PATH:/usr/local/mysql/bin/"
export PATH

You could also add some code to bin/activate that would search the system for common places that mysql_config can be found.


#10 Ori Peleg commented on :

Thanks, I was stuck on this exactly.


#11 Sultan commented on :

I've installed python-dev package and it worked for me


#12 uberj commented on :

Thanks.


#13 method commented on :

sudo apt-get install mysql-server mysql-client libmysqlclient15-dev python-dev

pip install -E mysql-python


#14 Karthic commented on :

Great info. Worked like a charm!


#15 Rawgeek commented on :

Thanks so much installing dependencies worked


#16 sframe commented on :

I'm using pip in virtualenv on OS X (10.7) with MySQL installed via macports.

My issue was macports suffixed mysql_config as mysql_config5.

Solution for me:

sudo ln -s /opt/local/bin/mysql_config5 /opt/local/bin/mysql_config

Hope that helps anyone else in a similar situation.


#17 crw commented on :

This also works on Precise Pangolin (12.04). Weird.


#18 ando commented on :

pip install -E default/ MySQL-python ... no such option: -E


#20 Susant Shrestha commented on :

just do these steps.....
1.sudo apt-get install libmysqlclient-dev
2.pip install flask-MySQLdb
these have worked for me .... hope it will work for you as well

disqus:2674505187