python报错Could not open PYTHONSTARTUP

时间:2022-01-05 13:47:10

root@liqian-python:/pythonShare/monitor/m_server/core# python
Python 2.7.10 (default, Oct 14 2015, 16:09:02)
[GCC 5.2.1 20151010] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Could not open PYTHONSTARTUP
IOError: [Errno 2] No such file or directory: '/root/.pystartup'

解决方案:

1.创建文件并添加内容

vi /root/.pystartup

import sys
import readline
import rlcompleter
import atexit
import os
readline.parse_and_bind('tab: complete')
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
 
2.执行如下命令:

echo "export PYTHONSTARTUP=~/.pystartup" >> ~/.bashrc
source /root/.bashrc

再次进入python编辑器就不报错了!