python2.7在mac和linux 安装MySQL-python-1.2.5的错误解决方法
作者:程序员CKeen
博客:http://ckeen.cn
长期坚持做有价值的事!积累沉淀,持续成长,升维思考!希望把编码作为长期兴趣爱好😄
本文是在部署一个python 2.7开发环境下,使用tornado框架编写的web应用时,遇到安装MySQL-python-1.2.5报错,从而将解决的方法记录的下来的。因为我使用的mac,而服务器使用的ubuntu,所以这里我们会介绍mac和ubuntu环境下解决方法。
1.macOS的MySQL-python-1.2.5
首先我们使用pip install -r requirements.txt安装的时候,当安装到MySQL-python-1.2.5的时候报错,报错如下:
ERROR: Command errored out with exit status 1:
command: /Users/ckeen/miniconda3/envs/ckeen-python2.7/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/1h/44y767c13nd0tr10mk8zm_3h0000gn/T/pip-install-6DxliQ/MySQL-python/setup.py'"'"'; __file__='"'"'/private/var/folders/1h/44y767c13nd0tr10mk8zm_3h0000gn/T/pip-install-6DxliQ/MySQL-python/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/1h/44y767c13nd0tr10mk8zm_3h0000gn/T/pip-install-6DxliQ/MySQL-python/pip-egg-info
cwd: /private/var/folders/1h/44y767c13nd0tr10mk8zm_3h0000gn/T/pip-install-6DxliQ/MySQL-python/
Complete output (10 lines):
sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/1h/44y767c13nd0tr10mk8zm_3h0000gn/T/pip-install-6DxliQ/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
这里我们可以看到,提示找不到mysql_config, 而mysql_config是mysql的一个工具,所以这里我们需要先安装mysql,
1.安装mysql 5.7
这里我们使用mac的brew工具,来安装mysql 5.7版本,我们做如下操作:
$ tools git:(master) ? brew search mysql
==> Formulae
automysqlbackup mysql-client@5.7 mysql@5.6
mysql mysql-connector-c++ mysql@5.7
mysql++ mysql-sandbox mysqltuner
mysql-client mysql-search-replace qt-mysql
$ tools git:(master) ? brew install mysql@5.7
这里我们先通过brew查找到mysql, 然后使用install命令对mysql@5.7版本进行安装, 安装完成后我们使用如下命令查看mysql是否安装成功
$ tools git:(master) ? brew list
==> Formulae
ca-certificates icu4c mpdecimal python@3.9 zsh
cocoapods libcbor mysql@5.7 readline zstd
gdbm libevent ncurses six
gettext libffi openssl@1.1 sqlite
glib libfido2 pcre tree
glib-openssl lz4 protobuf xz
我们可以看到mysql@5.7已经安装成功, 我们使用find命令来查一下mysql_config
? tools git:(master) ? find / -name mysql_config
find: /usr/sbin/authserver: Permission denied
/usr/local/Cellar/mysql@5.7/5.7.37/bin/mysql_config
可以看到mysql_config已经安装成功,我们再次pip install -r requirements.txt安装,依然是上面的报错信息。在网上搜了一下, 说是mysql_config路径问题, 于是只有放弃使用pip进行安装,而是直接去找源码进行安装。
2. 使用MySQL-python-1.2.5源码进行安装
我们去Pypi的包管理网站搜索到MySQL-python-1.2.5的源码
源码地址: https://pypi.org/project/MySQL-python/#files
下载解压后, 在setup_posix.py文件中, 我们可以找到配置mysql_config的路径,我们进行如下修改:
#mysql_config.path = "mysql_config"
mysql_config.path = "/usr/local/Cellar/mysql@5.7/5.7.37/bin/mysql_config"
我们将mysql_config.path的路径改成我们本地的mysql_config工具的地址,然后在包里面执行安装命令:
$ python setup.py install
结果还是报错, 但是这次报错信息跟前面的不同,报错信息如下:
gcc -fno-strict-aliasing -I/Users/ckeen/miniconda3/envs/ckeen-python2.7/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql/8.0.28_1/include/mysql -I/Users/ckeen/miniconda3/envs/ckeen-python2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.7-x86_64-2.7/_mysql.o
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
^~~~~~~~~~~~~
1 error generated.
error: command 'gcc' failed with exit status 1
----------------------------------------
ERROR: Failed building wheel for MySQL-python
这说明mysql_config的路径是没问题,这里编译MySQL-python-1.2.5还需要引入my_config.h头文件, 于是我们继续使用find命令进行搜索
$ find / -name my_config.h
/usr/local/Cellar/mysql@5.7/5.7.37/include/mysql/my_config.h
我们发现my_config.h头文件在mysql的安装目录中也是有的,看来只是头文件不在编译时查找的lib目录,如是我给该目录设置了一个软连接,执行命令如下:
$ sudo ln -s /usr/local/Cellar/mysql@5.7/5.7.37/include/mysql/*.h /usr/local/include/
注: 这里按道理将mysql的include目录配置到系统的include目录中是比较好的方法
我们继续使用上述的python安装命令进行安装,结果又发现如下错误:
gcc -fno-strict-aliasing -I/Users/ckeen/miniconda3/envs/ckeen-python2.7/include -arch x86_64 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/local/Cellar/mysql-client@5.7/5.7.34/include/mysql -I/Users/ckeen/miniconda3/envs/ckeen-python2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.7-x86_64-2.7/_mysql.o
gcc -bundle -undefined dynamic_lookup -L/Users/ckeen/miniconda3/envs/ckeen-python2.7/lib -arch x86_64 -arch x86_64 build/temp.macosx-10.7-x86_64-2.7/_mysql.o -L/usr/local/Cellar/mysql-client@5.7/5.7.34/lib -L/Users/ckeen/miniconda3/envs/ckeen-python2.7/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.7-x86_64-2.7/_mysql.so
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'gcc' failed with exit status 1
错误提示中时在编译的时候引用了ssl和crypto库, 我们通过搜索ssl在openssl的库查找到libssl的文件和libcrypto文件,将这两个文件软连接添加到/usr/local/lib/目录。
$ sudo ln -s /usr/local/Cellar/openssl@1.1/1.1.1n/lib/libssl.a /usr/local/lib/libssl.a
$ sudo ln -s /usr/local/Cellar/openssl@1.1/1.1.1n/lib/libcrypto.a /usr/local/lib/libcrypto.a
再进行打包安装提示安装成功:
Installed /root/miniconda3/envs/ckeen-python2.7/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg
Processing dependencies for MySQL-python==1.2.5
Finished processing dependencies for MySQL-python==1.2.5
2. linux环境下安装MySQL-python-1.2.5
有了mac下安装报错的解决问题的经验,在ubuntu系统中,我们就直接使用使用源码进行安装了,解压包之后,直接使用如下命令进行安装:
$ python setup.py install
ubuntu报错信息:
Traceback (most recent call last):
File "web_server.py", line 15, in <module>
import asynchronous_response
File "/var/www/ckeen/web_server/bin/asynchronous_response.py", line 11, in <module>
import globalvar as GlobalVar
File "/var/www/ckeen/web_server/bin/globalvar.py", line 19, in <module>
import my_torndb
File "/var/www/ckeen/web_server/bin/my_torndb.py", line 1, in <module>
import torndb
File "/root/miniconda3/envs/ckeen-python2.7/lib/python2.7/site-packages/torndb.py", line 33, in <module>
import MySQLdb.constants
ImportError: No module named MySQLdb.constants
这个我们直接通过ubuntu的包管理工具apt安装mysqlclient的lib库
$ apt-get install libmysqlclient-dev
再进行打包安装提示安装成功
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!