Forums

mysql-python in virtualenv

Hi, how can I install mysql-python under my virtualenv? I know, it needs gcc compiler, and I also know compiler is absent. Is there a way to use global module in virtualenv?

mysql-python should be present already -- unless you created the virtualenv with --no-site-packages. If you did that, I'm not quite sure what the right solution is (as presumably you only want to use mysql-python and not the other site packages).

Perhaps the best solution would be for us to make the gcc compiler available?

I am having the same problem and I'm pretty sure I didn't create my virtualenv with --no-site-packages. I actually followed the steps described here: http://interestinginnit.blogspot.com.br/2012/07/install-django-14-in-python-anywhere.html

When I try to install mysql-python from pip, I get the following error:

Downloading/unpacking mysql-python
Downloading MySQL-python-1.2.3.tar.gz (70kB): 70kB downloaded
Running setup.py egg_info for package mysql-python
sh: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/home/felipemartim/.virtualenvs/django/build/mysql-python/setup.py", line 15, in <module>
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

@felipemartim: First of all Welcome to PA. We're glad you are here.

Second, I'm a bit confused. Why are you using pip on MySQL-python? It's part of the battery's included for PA. You should just be able to import it and use it. Could you provide more information or clarification?

In [1]: import MySQLdb
In [2]: MySQLdb.version_info
Out[2]: (1, 2, 3, 'final', 0)
In [3]:

Thanks, a2j -- that's right. @felipemartim -- could you try just importing MySQLdb without pip installing it? It should just work.

Thanks for the help. I'm a complete newbie in python and this is probably something I did wrong, but the problem I'm having is that I can't even run

python manage.py syncdb

When I try to import MySQLdb in a python shell i get the following:

(django)16:46 ~/virtualoffice (master)$ python
Python 2.7.3 (default, Sep 17 2012, 17:42:49)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb
>>>

And that's the same error I get when running syncdb. That's why I'm trying to install it.

That's odd, it definitely looks like your virtualenv is excluding system packages. Could you show us what PYTHONPATH is set to? that is, while in your django virtualenv, run

(django)16:46 ~/virtualoffice (master)$ echo $PYTHONPATH

I checked PYTHONPATH variable and it was empty by default (don't know why). So I added some paths to it and it is now like this

(django)21:46 ~/virtualoffice (master)$ echo $PYTHONPATH
/usr/local/lib/python2.7:~/Envs/django/build/mysql-python

The error was:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

and now that I changed PYTHONPATH I'm getting this:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named _mysql

Do I have to include everything on PYTHONPATH or is there another way?

Hmm. It definitely does look as if the virtualenv has decided not to make any of the system-installed packages visible. I'll talk to my colleagues tomorrow morning and we'll see if we can work out what's wrong.

Ah, OK, it looks like one of the articles that talks about virtualenvwrapper is a bit misleading - it mentions the --no-site-packages flag, but in fact that's the default.

You have to actually pass --system-site-packages to keep the system packages.

So, do get all our packages, including MySqldb, and also upgrade Django, you need to:

export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
source virtualenvwrapper.sh
mkvirtualenv django14 --system-site-packages
workon django14
pip freeze | grep Django # will show 1.3
pip install --upgrade django
pip freeze | grep Django # will show 1.4
pip freeze | grep MySQL # will show MySQL-python is installed

@Harry, thank you very much! It worked like a charm.

Is there no way to do this without keeping the site packages? Also is there a plan for postgres support in the future?

Postgres support is on the cards yes, we can't really promise when though. The alternative to --system-site-packages would be to find the directory in /usr/lib that the currently installed version lives in, and then manually copy it into your virtualenv. It's a hack, but it just.. might... work...

I installed it without keeping site packages, by creating symbolic links in the virtualenv site-packages.

ln -s XXX myEnv/lib/python2.7/site-packages

so that the following links are made:

myEnv/lib/python2.7/site-packages/MySQLdb -> /usr/local/lib/python2.7/site-packages/MySQLdb/
myEnv/lib/python2.7/site-packages/_mysql.so -> /usr/local/lib/python2.7/site-packages/_mysql.so
myEnv/lib/python2.7/site-packages/_mysql_exceptions.py -> /usr/local/lib/python2.7/site-packages/_mysql_exceptions.py
myEnv/lib/python2.7/site-packages/_mysql_exceptions.pyc -> /usr/local/lib/python2.7/site-packages/_mysql_exceptions.pyc

To find the links that need to be made you can consult the egg description in the file

/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7.egg-info/installed-files.txt

I just ran into the same issue. It seems the PA has fixed this issue. I was able to successfully run

pip install mysql-python

in my virtual enviroment. Thank you for fixing this.

Yes, we added support for C compilation a couple of months back. Glad to hear it worked for you!