使用 mysql-db python 连接器

使用 mysql-db python 连接器

我最终能够安装连接器,如下所示:

如何从 16.04 上的 Python 3 连接到 MySQL 数据库

我在最后附上了安装的结果。

但是,当我尝试在某些 python3 代码中使用连接器时,它无法识别 mysqldb 包?

import mysqldb as mdb

pycharm中报错如下:

ModuleNotFoundError: No module named 'mysqldb'

这个错误是因为我在虚拟环境中设置了mysqldb吗?

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> 

答案1

它区分大小写。

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

安装了与您相同的包,并且似乎可以工作。

编辑:我错过了您在虚拟环境中使用它的机会!抱歉。

要在虚拟环境中使用站点包,您必须传递--system-site-packages给虚拟环境,例如

[~]$ 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
>>> 

相关内容