'VirtualEnv ImportError: No module named MySQLdb

I am receiving the error: ImportError: No module named MySQLdb whenever I try to run my local dev server and it is driving me crazy. I have tried everything I could find online:

  1. brew install mysql
  2. pip install mysqldb
  3. pip install mysql
  4. pip install mysql-python
  5. pip install MySQL-python
  6. easy_install mysql-python
  7. easy_install MySQL-python
  8. pip install mysqlclient

I am running out of options and can't figure out why I continue to receive this error. I am attempting to run my local dev server from using Google App Engine on a macOS Sierra system and I am using python version 2.7. I am also running: source env/bin/activate at the directory my project files are and am installing all dependencies there as well. My path looks like this:

/usr/local/bin/python:/usr/local/mysql/bin:/usr/local/opt/node@6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Frameworks/Python.framework/Versions/2.7/bin

Does anyone have further ideas I can attempt to resolve this issue?



Solution 1:[1]

When you are in the virtual env (source venv/bin/activate), just run in terminal:

sudo apt-get install python3-mysqldb
sudo apt-get install libmysqlclient-dev
pip install mysqlclient

You don't have to import anything in your py files.

The first one is just in case, but the other two work perfectly by themselves.

If you don't run libmysqlclient-dev, you won't be able to run any other mysql installation command while in the virtual environment.

Solution 2:[2]

Turns out I had the wrong python being pointed to in my virtualenv. It comes preinstalled with its own default python version and so, I created a new virtualenv and used the -p to set the python path to my own local python path.

Solution 3:[3]

In my project, with virtualenv, I just did

pip install mysqlclient

and like magic everything is ok

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emiliano Cervantes
Solution 2 crossfuse999
Solution 3 M E S A B O