Reputation: 39
I had posted about this error some time back but need some more clarification on this. I'm currently building out a Django Web Application using Visual Studio 2013 on a Windows 10 machine (Running Python3.4). While starting out I was constantly dealing with the MySQL connectivity issue, for which I did a mysqlclient pip-install. I had created two projects that use MySQL as a backend and after installing the mysqlclient I was able to connect to the database through the current project I was working on. When I opened the second project and tried to connect to the database, I got the same 'No Module called MySqlDB' error. Now, the difference between both projects was that the first one was NOT created within a Virtual Environment whereas the second was.
So I have come to deduce that projects created within the Python virtual environment are not able to connect to the database. Can someone here please help me in getting this problem solved. I need to know how the mysqlclient module can be loaded onto a virtual environment so that a project can use it. Thanks
Upvotes: 0
Views: 701
Reputation: 376
You can fix it by installing pymysql package
pip install pymysql
After installation succesfully completed, you have to add these lines into init.py
import pymysql
pymysql.install_as_MySQLdb()
It works for me
Upvotes: 2
Reputation: 39
This approach worked ! I was able to install the mysqlclient inside the virtual environment through the following command:-
python -m pip install mysqlclient
Thanks Much..!!!!!
Upvotes: 1
Reputation: 77902
So I have come to deduce that projects created within the Python virtual environment are not able to connect to the database
Given that virtualenv are a de-facto standard for web applications deployment, that would be quite surprising (hint: we maintain and host tens of Django projects using virtualenvs).
Can someone here please help me in getting this problem solved. I need to know how the mysqlclient module can be loaded onto a virtual environment so that a project can use it
The same way as with any python package: create your venv (if not done), activate it and pip install the package:
bruno@bigb:~/Work/playground$ virtualenv venv
New python executable in venv/bin/python
Installing distribute.............................................................................................................................................................................................done.
Installing pip...............done.
bruno@bigb:~/Work/playground$ source venv/bin/activate
(venv)bruno@bigb:~/Work/playground$ pip install mysqlclient
Downloading/unpacking mysqlclient
Downloading mysqlclient-1.3.6.tar.gz (78Kb): 78Kb downloaded
Running setup.py egg_info for package mysqlclient
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,3,6,'final',1) -D__version__=1.3.6 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g
In file included from /usr/include/python2.7/Python.h:8:0,
from _mysql.c:40:
/usr/include/python2.7/pyconfig.h:1161:0: attention : « _POSIX_C_SOURCE » redéfini [enabled by default]
/usr/include/features.h:164:0: note: ceci est la localisation d'une précédente définition
/usr/include/python2.7/pyconfig.h:1183:0: attention : « _XOPEN_SOURCE » redéfini [enabled by default]
/usr/include/features.h:166:0: note: ceci est la localisation d'une précédente définition
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
Successfully installed mysqlclient
Cleaning up...
(venv)bruno@bigb:~/Work/playground$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
pythonrc start
pythonrc done
>>> import MySQLdb
>>> >>> MySQLdb
<module 'MySQLdb' from '/home/bruno/Work/playground/venv/local/lib/python2.7/site-packages/MySQLdb/__init__.pyc'>
Upvotes: 2