第一步:查看python系统路径
[[email protected] 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 ###############################################################
[[email protected] tools]# cd /usr/local/lib/python2.7/site-packages [[email protected] 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 [[email protected] site-packages]# chmod +x startup.py [[email protected] site-packages]# cd [[email protected] ~]# 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(); [[email protected] ~]#
解决方法:
这是因为你安装的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
第四步:可以在系统环境变量中,加入读取路径,这样就免去了每次导入的麻烦
[[email protected] python2.7]# cat ~/.bashrc export PYTHONSTARTUP=/usr/local/lib/python2.7/site-packages/startup.py
即可。
完美解决
[[email protected] 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 >>> >>>
时间: 2024-10-08 20:33:22