首先,MySQLdb这个包在python2版本是可以直接 pip install MySQLdb 下载的,但是,python3是不支持这么干的。
其次,python3对于这个包的下载用的是另一个第三方包的名称:mysqlclient
接下来看几个报错信息:
(py38env) ymengkai@ubuntu:~/p4/scm_project/build$ python3
Python 3.8.1 (default, Jun 27 2022, 19:45:39)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MySQLdb'
>>>
(py38env) ymengkai@ubuntu:~/p4/scm_project/build$ pip install mysqlclient
Looking in indexes: https://build-artifactory.eng.vmware.com/artifactory/api/pypi/pypi-virtual/simple
Collecting mysqlclient
Downloading https://build-artifactory.eng.vmware.com/artifactory/api/pypi/pypi-virtual/packages/packages/50/5f/eac919b88b9df39bbe4a855f136d58f80d191cfea34a3dcf96bf5d8ace0a/mysqlclient-2.1.1.tar.gz (88 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.1/88.1 kB 397.7 kB/s eta 0:00:00
Preparing metadata (setup.py) ... error
error: subprocess-exited-with-error
× python setup.py egg_info did not run successfully.
│ exit code: 1
╰─> [16 lines of output]
/bin/sh: 1: mysql_config: not found
/bin/sh: 1: mariadb_config: not found
/bin/sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-xeoyrqi2/mysqlclient_1d4d271ae9ca47de88613e25418e3780/setup.py", line 15, in <module>
metadata, options = get_config()
File "/tmp/pip-install-xeoyrqi2/mysqlclient_1d4d271ae9ca47de88613e25418e3780/setup_posix.py", line 70, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-xeoyrqi2/mysqlclient_1d4d271ae9ca47de88613e25418e3780/setup_posix.py", line 31, in mysql_config
raise OSError("{} not found".format(_mysql_config_path))
OSError: mysql_config not found
mysql_config --version
mariadb_config --version
mysql_config --libs
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed
× Encountered error while generating package metadata.
╰─> See above for output.
note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
发现下不下来,还是有问题,但是问题是mysql_config,mariadb_config,mysql_config找不到,具体错误是,OSError: mysql_config not found。
接下来解决这个问题:
执行命令:sudo apt-get install libmysqlclient-dev
然后就ok了。
再次下载mysqlclient包:
(py38env) ymengkai@ubuntu:~/p4/scm_project/build$ pip install mysqlclient
Looking in indexes: https://build-artifactory.eng.vmware.com/artifactory/api/pypi/pypi-virtual/simple
Collecting mysqlclient
Using cached https://build-artifactory.eng.vmware.com/artifactory/api/pypi/pypi-virtual/packages/packages/50/5f/eac919b88b9df39bbe4a855f136d58f80d191cfea34a3dcf96bf5d8ace0a/mysqlclient-2.1.1.tar.gz (88 kB)
Preparing metadata (setup.py) ... done
Installing collected packages: mysqlclient
DEPRECATION: mysqlclient is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-2.1.1
验证下:
(py38env) ymengkai@ubuntu:~/p4/scm_project/build$ python3
Python 3.8.1 (default, Jun 27 2022, 19:45:39)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>
这就ok了....
但是有一点注意下,事实上,在python3中,基本是不再使用MySQLdb包了,正如上面看到的,会很麻烦,不仅需要 pip 去下载一些包,关键是还需要对平台做一些操作,问题是,这样以来,若是跨平台了,又得重新倒腾,哪怕不跨平台,就是换一个机子,那也得再倒腾一次。所以很不方便,因此,最好的做法就是换掉它,直接用pymysql就好了。