9

I'm using the most recent versions of all software (Django, Python, virtualenv, MySQLdb) and I can't get this to work. When I run "import MySQLdb" in the python prompt from outside of the virtualenv, it works, inside it says "ImportError: No module named MySQLdb".

I'm trying to learn Python and Linux web development. I know that it's easiest to use SQLLite, but I want to learn how to develop larger-scale applications comparable to what I can do in .NET. I've read every blog post on Google and every post here on StackOverflow and they all suggest that I run "sudo pip install mysql-python" but it just says "Requirement already satisfied: mysql-python in /usr/lib/pymodules/python2.7"

Any help would be appreciated! I'm stuck over here and don't want to throw in the towel and just go back to doing this on Microsoft technologies because I can't even get a basic dev environment up and running.

dudemonkey
  • 1,091
  • 5
  • 15
  • 26

3 Answers3

14

If you have created the virtualenv with the --no-site-packages switch (the default), then system-wide installed additions such as MySQLdb are not included in the virtual environment packages.

You need to install MySQLdb with the pip command installed with the virtualenv. Either activate the virtualenv with the bin/activate script, or use bin/pip from within the virtualenv to install the MySQLdb library locally as well.

Alternatively, create a new virtualenv with system site-packages included by using the --system-site-package switch.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Another way would be to manually symlink to the system-wide ``_mysql.so``, ``_mysql_exceptions.py`` and ``MySQLdb`` (directory) from your virtualenv ``site-packages`` directory. – codeape Nov 08 '12 at 11:24
  • creating a new virtualenv with the --system-site-package works. Is there any way to modify an existing one? – dudemonkey Nov 08 '12 at 11:29
  • @dudemonkey: As I stated, install the package in the virtualenv with the virtualenv pip, or use codeape's suggestion and create symlinks. – Martijn Pieters Nov 08 '12 at 11:32
  • 1
    @MartijnPieters I am getting a `mysql_config not found` error when installing from inside virtualenv – ravi404 Apr 19 '13 at 15:17
  • 1
    You need the MySQL development files (headers, `mysql_config` command). Make sure you have those installed. On Ubuntu and Debian those are part of the `libmysqlclient-dev` package, for example. – Martijn Pieters Apr 19 '13 at 15:23
  • @MartijnPieters I installed `libmysqlclient-dev`. But now gettin the error `Python.h: No such file or directory` while installing with `easy_install MySQL-python` – ravi404 Apr 19 '13 at 15:43
  • You need the same thing for Python. Ubuntu / Debian has `python2.7-dev` for Python 2.7, for example. – Martijn Pieters Apr 19 '13 at 15:47
  • I have just started using ubuntu and python. Finding it difficult to set this up. – ravi404 Apr 19 '13 at 16:03
  • @MartijnPieters +1. Thanks! what I missed was `apt-get update` – ravi404 Apr 19 '13 at 16:49
  • @MartijnPieters I am coming at this from the opposite direction as the OP. Can I install a MySQL Server inside a VirtualEnv? Right now, running my code involves installing MYSQL Server on the system first, and that's a pain for users. – john_science Apr 30 '15 at 18:34
1
  1. source $ENV_PATH/bin/activate
  2. pip uninstall MySQL-python
  3. pip install MySQL-python

this worked for me.

Gaurav Vasudev
  • 117
  • 1
  • 10
fwindpeak
  • 11
  • 1
0

I went through same problem, but using pip from virtualenv didn't solve the problem as I got this error

error: could not delete '/Library/Python/2.7/site-packages/_mysql.so': Permission denied

Earlier I had installed the package by sudo pip install mysql-python

To solve, copy files /Library/Python/2.7/site-packages/MySQL_python-1.2.5-py2.7.egg-info and /Library/Python/2.7/site-packages/_mysql* to ~/v/lib/python-2.7/site-packages and include /usr/local/mysql/lib in DYLD_LIBRARY_PATH env variable.

For the second step I am doing export DYLD_LIBRARY_PATH=/usr/local/mysql/lib in ~/.profile

lafolle
  • 99
  • 1
  • 7