Usando o conector python mysql-db

0

Eu finalmente consegui instalar o conector como mostrado abaixo:

Como conectar-se ao MySQL db de python 3 em 16.04

Eu incluo o resultado da instalação no final.

No entanto, quando tento usar o conector em algum código python3, ele não reconhece o pacote mysqldb?

import mysqldb as mdb

O erro dado no pycharm é:

ModuleNotFoundError: No module named 'mysqldb'

Esse erro é porque eu configurei o mysqldb em um ambiente virtual?

Resultados da instalação do mysqldb:

(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> 
    
por Bazman 27.12.2017 / 21:04

1 resposta

1

É sensível a maiúsculas e minúsculas.

$ 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
>>> 

Instalou o mesmo pacote que você e parece funcionar.

Edit: Eu senti falta do que você estava usando dentro de um ambiente virtual! Desculpe.

Para usar pacotes de sites em um virtualenv, você precisa passar --system-site-packages para virtualenv, por exemplo

[~]$ 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
>>> 
    
por vidarlo 27.12.2017 / 22:24