系统环境:
[root@agent ~]# cat /etc/redhat-release CentOS release 6.4 (Final) [root@agent ~]# uname -a Linux agent.test.com 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
需求:安装python环境
软件包的下载地址:https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz
一、具体安装:
tar zxf Python-2.7.10.tgz cd Python-2.7.10 ./configure --prefix=/usr/local/python make && make install
二、启动python2.7:
[root@agent2 ~]# /usr/local/python/bin/python2.7
Python 2.7.10 (default, Jul 10 2015, 03:54:03) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last): File "/root/.pythonstartup", line 2, in <module> import readline ImportError: No module named readline 排查思路:从上述报错可知:ImportError: No module named readline,缺少readline模块 解决方法:安装readline-6.2.4.1.tar.gz 下载地址:https://pypi.python.org/packages/source/r/readline/readline-6.2.4.1.tar.gz#md5=578237939c81fdbc2c8334d168b17907 安装readline [root@agent2 ~]# tar zxf readline-6.2.4.1.tar.gz -C /usr/src/ [root@agent2 ~]# cd /usr/src/readline-6.2.4.1/ [root@agent2 readline-6.2.4.1]# python setup.py build Traceback (most recent call last): File "setup.py", line 8, in <module> from setuptools import setup, Extension ImportError: No module named setuptools 从上述报错信息可知:ImportError: No module named setuptools,缺少setuptools模块 解决方法:安装setuptools 下载地址:http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz [root@agent2 ~]# tar zxf setuptools-0.6c11.tar.gz -C /usr/src/ [root@agent2 ~]# cd /usr/src/setuptools-0.6c11/ [root@agent2 setuptools-0.6c11]# python setup.py build [root@agent2 setuptools-0.6c11]# python setup.py install
setuptools安装完成后,进入到readline相关的目录下,执行如下操作:
[root@agent2 readline-6.2.4.1]# python setup.py build 再次readline报错内容如下: ./build.sh: line 41: patch: command not found ============ Building the readline extension module ============ running build running build_ext building 'readline' extension creating build creating build/temp.linux-x86_64-2.6 creating build/temp.linux-x86_64-2.6/Modules creating build/temp.linux-x86_64-2.6/Modules/2.x gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DHAVE_RL_CALLBACK -DHAVE_RL_CATCH_SIGNAL -DHAVE_RL_COMPLETION_APPEND_CHARACTER -DHAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK -DHAVE_RL_COMPLETION_MATCHES -DHAVE_RL_COMPLETION_SUPPRESS_APPEND -DHAVE_RL_PRE_INPUT_HOOK -I. -I/usr/include/python2.6 -c Modules/2.x/readline.c -o build/temp.linux-x86_64-2.6/Modules/2.x/readline.o -Wno-strict-prototypes Modules/2.x/readline.c:8:20: error: Python.h: No such file or directory In file included from ./readline/readline.h:36, from Modules/2.x/readline.c:31: ./readline/rltypedefs.h:64: error: expected ‘)’ before ‘*’ token In file included from Modules/2.x/readline.c:31: ./readline/readline.h:419: error: expected ‘)’ before ‘*’ token ./readline/readline.h:542: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token ./readline/readline.h:543: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token ./readline/readline.h:565: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token ./readline/readline.h:867: error: expected specifier-qualifier-list before ‘FILE’ Modules/2.x/readline.c:74: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:92: error: expected ‘)’ before string constant Modules/2.x/readline.c:98: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:111: error: expected ‘)’ before string constant Modules/2.x/readline.c:118: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:132: error: expected ‘)’ before string constant Modules/2.x/readline.c:139: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:154: error: expected ‘)’ before string constant Modules/2.x/readline.c:161: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:172: error: expected ‘)’ before string constant Modules/2.x/readline.c:180: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:187: error: expected ‘)’ before string constant Modules/2.x/readline.c:194: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:225: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:226: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:229: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:232: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:253: error: expected ‘)’ before string constant Modules/2.x/readline.c:259: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:266: error: expected ‘)’ before string constant Modules/2.x/readline.c:276: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:283: error: expected ‘)’ before string constant Modules/2.x/readline.c:294: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:296: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:297: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:301: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:308: error: expected ‘)’ before string constant Modules/2.x/readline.c:314: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:322: error: expected ‘)’ before string constant Modules/2.x/readline.c:328: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:336: error: expected ‘)’ before string constant Modules/2.x/readline.c:342: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:356: error: expected ‘)’ before string constant Modules/2.x/readline.c: In function ‘_py_free_history_entry’: Modules/2.x/readline.c:372: warning: implicit declaration of function ‘free’ Modules/2.x/readline.c:372: warning: incompatible implicit declaration of built-in function ‘free’ Modules/2.x/readline.c: At top level: Modules/2.x/readline.c:391: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:417: error: expected ‘)’ before string constant Modules/2.x/readline.c:420: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:449: error: expected ‘)’ before string constant Modules/2.x/readline.c:454: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:467: error: expected ‘)’ before string constant Modules/2.x/readline.c:473: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:480: error: expected ‘)’ before string constant Modules/2.x/readline.c:486: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:493: error: expected ‘)’ before string constant Modules/2.x/readline.c:500: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:511: error: expected ‘)’ before string constant Modules/2.x/readline.c: In function ‘_py_get_history_length’: Modules/2.x/readline.c:530: warning: incompatible implicit declaration of built-in function ‘free’ Modules/2.x/readline.c: At top level: Modules/2.x/readline.c:536: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:573: error: expected ‘)’ before string constant Modules/2.x/readline.c:579: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:586: error: expected ‘)’ before string constant Modules/2.x/readline.c:592: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:599: error: expected ‘)’ before string constant Modules/2.x/readline.c:607: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:615: error: expected ‘)’ before string constant Modules/2.x/readline.c:622: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:633: error: expected ‘)’ before string constant Modules/2.x/readline.c:639: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token Modules/2.x/readline.c:647: error: expected ‘)’ before string constant Modules/2.x/readline.c:654: error: array type has incomplete element type Modules/2.x/readline.c:656: error: ‘parse_and_bind’ undeclared here (not in a function) Modules/2.x/readline.c:656: error: ‘METH_VARARGS’ undeclared here (not in a function) Modules/2.x/readline.c:656: error: ‘doc_parse_and_bind’ undeclared here (not in a function) Modules/2.x/readline.c:657: error: ‘get_line_buffer’ undeclared here (not in a function) Modules/2.x/readline.c:657: error: ‘METH_NOARGS’ undeclared here (not in a function) Modules/2.x/readline.c:657: error: ‘doc_get_line_buffer’ undeclared here (not in a function) Modules/2.x/readline.c:658: error: ‘insert_text’ undeclared here (not in a function) Modules/2.x/readline.c:658: error: ‘doc_insert_text’ undeclared here (not in a function) Modules/2.x/readline.c:659: error: ‘redisplay’ undeclared here (not in a function) Modules/2.x/readline.c:659: error: ‘doc_redisplay’ undeclared here (not in a function) Modules/2.x/readline.c:660: error: ‘read_init_file’ undeclared here (not in a function) Modules/2.x/readline.c:660: error: ‘doc_read_init_file’ undeclared here (not in a function) Modules/2.x/readline.c:661: error: ‘read_history_file’ undeclared here (not in a function) Modules/2.x/readline.c:662: error: ‘doc_read_history_file’ undeclared here (not in a function) Modules/2.x/readline.c:663: error: ‘write_history_file’ undeclared here (not in a function) Modules/2.x/readline.c:664: error: ‘doc_write_history_file’ undeclared here (not in a function) Modules/2.x/readline.c:665: error: ‘get_history_item’ undeclared here (not in a function) Modules/2.x/readline.c:666: error: ‘doc_get_history_item’ undeclared here (not in a function) Modules/2.x/readline.c:667: error: ‘PyCFunction’ undeclared here (not in a function) Modules/2.x/readline.c:667: error: expected ‘}’ before ‘get_current_history_length’ Modules/2.x/readline.c:669: error: ‘set_history_length’ undeclared here (not in a function) Modules/2.x/readline.c:670: error: ‘set_history_length_doc’ undeclared here (not in a function) Modules/2.x/readline.c:671: error: ‘get_history_length’ undeclared here (not in a function) Modules/2.x/readline.c:672: error: ‘get_history_length_doc’ undeclared here (not in a function) Modules/2.x/readline.c:673: error: ‘set_completer’ undeclared here (not in a function) Modules/2.x/readline.c:673: error: ‘doc_set_completer’ undeclared here (not in a function) Modules/2.x/readline.c:674: error: ‘get_completer’ undeclared here (not in a function) Modules/2.x/readline.c:674: error: ‘doc_get_completer’ undeclared here (not in a function) Modules/2.x/readline.c:675: error: ‘get_completion_type’ undeclared here (not in a function) Modules/2.x/readline.c:676: error: ‘doc_get_completion_type’ undeclared here (not in a function) Modules/2.x/readline.c:677: error: ‘get_begidx’ undeclared here (not in a function) Modules/2.x/readline.c:677: error: ‘doc_get_begidx’ undeclared here (not in a function) Modules/2.x/readline.c:678: error: ‘get_endidx’ undeclared here (not in a function) Modules/2.x/readline.c:678: error: ‘doc_get_endidx’ undeclared here (not in a function) Modules/2.x/readline.c:680: error: ‘set_completer_delims’ undeclared here (not in a function) Modules/2.x/readline.c:681: error: ‘doc_set_completer_delims’ undeclared here (not in a function) Modules/2.x/readline.c:682: error: ‘py_add_history’ undeclared here (not in a function) Modules/2.x/readline.c:682: error: ‘doc_add_history’ undeclared here (not in a function) Modules/2.x/readline.c:683: error: ‘py_remove_history’ undeclared here (not in a function) Modules/2.x/readline.c:683: error: ‘doc_remove_history’ undeclared here (not in a function) Modules/2.x/readline.c:684: error: ‘py_replace_history’ undeclared here (not in a function) Modules/2.x/readline.c:684: error: ‘doc_replace_history’ undeclared here (not in a function) Modules/2.x/readline.c:685: error: ‘get_completer_delims’ undeclared here (not in a function) Modules/2.x/readline.c:686: error: ‘doc_get_completer_delims’ undeclared here (not in a function) Modules/2.x/readline.c:688: error: ‘set_completion_display_matches_hook’ undeclared here (not in a function) Modules/2.x/readline.c:689: error: ‘doc_set_completion_display_matches_hook’ undeclared here (not in a function) Modules/2.x/readline.c:690: error: ‘set_startup_hook’ undeclared here (not in a function) Modules/2.x/readline.c:691: error: ‘doc_set_startup_hook’ undeclared here (not in a function) Modules/2.x/readline.c:693: error: ‘set_pre_input_hook’ undeclared here (not in a function) Modules/2.x/readline.c:694: error: ‘doc_set_pre_input_hook’ undeclared here (not in a function) Modules/2.x/readline.c:697: error: ‘py_clear_history’ undeclared here (not in a function) Modules/2.x/readline.c:697: error: ‘doc_clear_history’ undeclared here (not in a function) Modules/2.x/readline.c:706: error: expected ‘)’ before ‘*’ token Modules/2.x/readline.c: In function ‘on_startup_hook’: Modules/2.x/readline.c:741: warning: implicit declaration of function ‘on_hook’ Modules/2.x/readline.c:741: error: ‘startup_hook’ undeclared (first use in this function) Modules/2.x/readline.c:741: error: (Each undeclared identifier is reported only once Modules/2.x/readline.c:741: error: for each function it appears in.) Modules/2.x/readline.c: In function ‘on_pre_input_hook’: Modules/2.x/readline.c:748: error: ‘pre_input_hook’ undeclared (first use in this function) Modules/2.x/readline.c: In function ‘on_completion_display_matches_hook’: Modules/2.x/readline.c:760: error: ‘PyObject’ undeclared (first use in this function) Modules/2.x/readline.c:760: error: ‘m’ undeclared (first use in this function) Modules/2.x/readline.c:760: error: ‘s’ undeclared (first use in this function) Modules/2.x/readline.c:760: warning: left-hand operand of comma expression has no effect Modules/2.x/readline.c:760: error: ‘r’ undeclared (first use in this function) Modules/2.x/readline.c:760: warning: left-hand operand of comma expression has no effect Modules/2.x/readline.c:764: warning: implicit declaration of function ‘PyList_New’ Modules/2.x/readline.c:768: warning: implicit declaration of function ‘PyString_FromString’ Modules/2.x/readline.c:771: warning: implicit declaration of function ‘PyList_SetItem’ Modules/2.x/readline.c:775: warning: implicit declaration of function ‘PyObject_CallFunction’ Modules/2.x/readline.c:775: error: ‘completion_display_matches_hook’ undeclared (first use in this function) Modules/2.x/readline.c:778: warning: implicit declaration of function ‘Py_DECREF’ Modules/2.x/readline.c:781: error: ‘Py_None’ undeclared (first use in this function) Modules/2.x/readline.c:781: warning: implicit declaration of function ‘PyInt_AsLong’ Modules/2.x/readline.c:781: warning: implicit declaration of function ‘PyErr_Occurred’ Modules/2.x/readline.c:784: warning: implicit declaration of function ‘Py_XDECREF’ Modules/2.x/readline.c:788: warning: implicit declaration of function ‘PyErr_Clear’ Modules/2.x/readline.c: In function ‘on_completion’: Modules/2.x/readline.c:804: error: ‘completer’ undeclared (first use in this function) Modules/2.x/readline.c:805: error: ‘PyObject’ undeclared (first use in this function) Modules/2.x/readline.c:805: error: ‘r’ undeclared (first use in this function) Modules/2.x/readline.c:813: error: ‘Py_None’ undeclared (first use in this function) Modules/2.x/readline.c:817: warning: implicit declaration of function ‘PyString_AsString’ Modules/2.x/readline.c: In function ‘flex_complete’: Modules/2.x/readline.c:849: error: ‘begidx’ undeclared (first use in this function) Modules/2.x/readline.c:850: error: ‘endidx’ undeclared (first use in this function) Modules/2.x/readline.c:851: warning: implicit declaration of function ‘PyInt_FromLong’ Modules/2.x/readline.c: In function ‘setup_readline’: Modules/2.x/readline.c:900: error: ‘begidx’ undeclared (first use in this function) Modules/2.x/readline.c:901: error: ‘endidx’ undeclared (first use in this function) Modules/2.x/readline.c: In function ‘readline_until_enter_or_signal’: Modules/2.x/readline.c:1009: error: ‘PyOS_sighandler_t’ undeclared (first use in this function) Modules/2.x/readline.c:1009: error: expected ‘;’ before ‘old_inthandler’ Modules/2.x/readline.c:1014: error: ‘old_inthandler’ undeclared (first use in this function) Modules/2.x/readline.c:1014: warning: implicit declaration of function ‘PyOS_setsig’ Modules/2.x/readline.c:1024: error: ‘PyOS_InputHook’ undeclared (first use in this function) Modules/2.x/readline.c: At top level: Modules/2.x/readline.c:1034: error: expected ‘)’ before ‘*’ token Modules/2.x/readline.c:1111: error: expected ‘)’ before string constant Modules/2.x/readline.c:1119: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘initreadline’ error: command 'gcc' failed with exit status 1
解决方法:yum -y install gcc python-devel 根据实际情况来看,是否需要安装其他的软件包:zlib-devel openssl-devel libffi-devel (我的机器安装上,后来又移除了,python环境运行正常)
再次执行python setup.py build,报错内容如下:
============ Building the readline extension module ============ running build running build_ext building 'readline' extension gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -DHAVE_RL_CALLBACK -DHAVE_RL_CATCH_SIGNAL -DHAVE_RL_COMPLETION_APPEND_CHARACTER -DHAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK -DHAVE_RL_COMPLETION_MATCHES -DHAVE_RL_COMPLETION_SUPPRESS_APPEND -DHAVE_RL_PRE_INPUT_HOOK -I. -I/usr/include/python2.6 -c Modules/2.x/readline.c -o build/temp.linux-x86_64-2.6/Modules/2.x/readline.o -Wno-strict-prototypes gcc -pthread -shared build/temp.linux-x86_64-2.6/Modules/2.x/readline.o readline/libreadline.a readline/libhistory.a -L/usr/lib64 -lncurses -lpython2.6 -o build/lib.linux-x86_64-2.6/readline.so gcc: readline/libreadline.a:没有那个文件或目录 gcc: readline/libhistory.a:没有那个文件或目录
解决方法: yum -y install patch python setup.py build python setup.py install 至此readline总算是编译安装成功,如果此时执行/usr/local/python/bin/python2.7直接启动python,依然看到和首次启动python一样的错误,此时,别着急,重新编译安装python后再启动即可正常使用。 [root@agent Python-2.7.10]# /usr/local/python/bin/python2.7 Python 2.7.10 (default, Jul 10 2015, 15:41:26) [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
>>>
参考文章: http://www.pythontab.com/html/2012/pythongaoji_1220/21.html
http://blog.sina.com.cn/s/blog_55119d4d01017ijx.html
http://blog.csdn.net/fenglifeng1987/article/details/38057193
http://www.68idc.cn/help/jiabenmake/python/20150227234518.html