0

I was finally able to install the connector as shown below:

How to connect to MySQL db from python 3 on 16.04

I include the result of the install at the end.

However when I try to use the connector in some python3 code it does not recognize the mysqldb package?

import mysqldb as mdb

The error given in pycharm is:

ModuleNotFoundError: No module named 'mysqldb'

Is this error because I set up mysqldb in a virtual environment?

Results of the mysqldb install:

(env) tom@TP:~/SAT$ sudo apt install python3-mysqldb
[sudo] password for tom: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.10.0-28 linux-headers-4.10.0-28-generic
  linux-headers-4.10.0-37 linux-headers-4.10.0-37-generic
  linux-image-4.10.0-28-generic linux-image-4.10.0-37-generic
  linux-image-extra-4.10.0-28-generic linux-image-extra-4.10.0-37-generic
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  python-egenix-mxdatetime python3-mysqldb-dbg
The following NEW packages will be installed
  python3-mysqldb
0 to upgrade, 1 to newly install, 0 to remove and 23 not to upgrade.
Need to get 39.2 kB of archives.
After this operation, 161 kB of additional disk space will be used.
Get:1 http://gb.archive.ubuntu.com/ubuntu xenial/main amd64 python3-mysqldb amd64 1.3.7-1build2 [39.2 kB]
Fetched 39.2 kB in 0s (343 kB/s)         
Selecting previously unselected package python3-mysqldb.
(Reading database ... 286207 files and directories currently installed.)
Preparing to unpack .../python3-mysqldb_1.3.7-1build2_amd64.deb ...
Unpacking python3-mysqldb (1.3.7-1build2) ...
Setting up python3-mysqldb (1.3.7-1build2) ...


(env) tom@TP:~/SAT$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
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'

(env) tom@TP:~/SAT$ dpkg --get-selections | grep mysql
libmysqlclient-dev              install
libmysqlclient20:amd64              install
mysql-client-5.7                install
mysql-client-core-5.7               install
mysql-common                    install
mysql-server                    install
mysql-server-5.7                install
mysql-server-core-5.7               install
python3-mysqldb                 install


(env) tom@TP:~/SAT$ mysql
ERROR 1045 (28000): Access denied for user 'tom'@'localhost' (using password: NO)


tom@TP:~$ cd SAT
tom@TP:~/SAT$ virtualenv -p python3 --system-site-packages tmp/
Running virtualenv with interpreter /home/tom/anaconda3/bin/python3
Using base prefix '/home/tom/anaconda3'
New python executable in /home/tom/SAT/tmp/bin/python3
copying /home/tom/anaconda3/bin/python3 => /home/tom/SAT/tmp/bin/python3
copying /home/tom/anaconda3/bin/../lib/libpython3.6m.so.1.0 => /home/tom/SAT/tmp/lib/libpython3.6m.so.1.0
Also creating executable in /home/tom/SAT/tmp/bin/python
Installing setuptools, pip, wheel...done.


tom@TP:~/SAT$ source env/bin/activate
(env) tom@TP:~/SAT$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
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'


>>> exit()

(

env) tom@TP:~/SAT$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
1

It's case sensitive.

$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
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'
>>> import MySQLdb
>>> 

Installed same package as you, and seems to work.

Edit: I missed that you were using it inside a virtual environment! Sorry.

To use site packages inside a virtualenv, you have to pass --system-site-packages to virtualenv, e.g.

[~]$ virtualenv -p python3 --system-site-packages tmp/
[~]$ source tmp/bin/activate
(tmp) [~]$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 
| improve this answer | |
  • Hi there, I tried with the correct casing but it still is not working. I added the input code and the error message at the bottom on the question. – Bazman Dec 28 '17 at 15:28
  • I also tried a couple of ways of checking if mysql is installed I have included those two at the bottom of the email. The first methos seems to suggest that needs to be installed while the second method seems to suggest that there is a problem with the password? – Bazman Dec 28 '17 at 15:42
  • 1
    Updated with some information on using system packages inside virtualenv. Your other alternativ is do use pip to install it inside the virtualenv. – vidarlo Dec 28 '17 at 15:58
  • OK I tried this but unfortunately import MySQLdb still is not being recognized by python 3.5? However, mysql -u root -p does start mysql successfully inside or outside the virtual environment? I assume this means that mysqldb does not need to be pip installed again inside (or outside) the env? – Bazman Dec 28 '17 at 16:11
  • 1
    OK got it working using the solution shown here stackoverflow.com/questions/35190465/… by arcegk. Thanks for all your help in any case. – Bazman Dec 28 '17 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.