第一步:查看python系统路径
[root@localhost tools]# python Python 2.7.12 (default, Jul 4 2016, 02:08:58) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path [‘‘, ‘/usr/local/lib/python27.zip‘, ‘/usr/local/lib/python2.7‘, ‘/usr/local/lib/python2.7/plat-linux2‘, ‘/usr/local/lib/python2.7/lib-tk‘, ‘/usr/local/lib/python2.7/lib-old‘, ‘/usr/local/lib/python2.7/lib-dynload‘, ‘/usr/local/lib/python2.7/site-packages‘] >>> quit();
第二步:编写startup.py ,并加入到python系统路径中。
#!/usr/bin/python # python startup file import sys import readline import rlcompleter import atexit import os # tab completion readline.parse_and_bind(‘tab: complete‘) # history file histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘) try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter ###############################################################
[root@localhost tools]# cd /usr/local/lib/python2.7/site-packages [root@localhost site-packages]# ll total 8 -rw-r--r-- 1 root root 119 Jul 4 02:09 README -rw-r--r-- 1 root root 407 Jul 4 02:16 startup.py [root@localhost site-packages]# chmod +x startup.py [root@localhost site-packages]# cd [root@localhost ~]# python Python 2.7.12 (default, Jul 4 2016, 02:08:58) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import startup Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/startup.py", line 5, in <module> import readline ImportError: No module named readline
故障解决:
>>> import startup Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/startup.py", line 5, in <module> import readline ImportError: No module named readline >>> >>> quit(); [root@localhost ~]#
解决方法:
这是因为你安装的python没有readline模块,需安装一下。
1):需要下载readline
2):安装readline-6.3.tar.gz时,下载
wget http://ftp.gnu.org/gnu/readline/readline-6.3.tar.gz tar xf readline-6.3.tar.gz cd readline-6.3 ./configure make make install
2):重新编译Python
cd Python-2.6.6 make install
第四步:可以在系统环境变量中,加入读取路径,这样就免去了每次导入的麻烦
[root@python python2.7]# cat ~/.bashrc export PYTHONSTARTUP=/usr/local/lib/python2.7/site-packages/startup.py
即可。
完美解决
[root@localhost Python-2.7.12]# python Python 2.7.12 (default, Jul 4 2016, 02:08:58) [GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> import sys >>> >>>
本文出自 “奋斗吧” 博客,谢绝转载!
原文地址:http://lvnian.blog.51cto.com/7155281/1795533