已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

【问题】

在折腾:

【已解决】Sublime Text 2中运行Python程序出错:The system cannot find the file specified

的过程中,虽然解决了找不到python.exe的问题,但是又出现下面这个错误:

Demo print in Sublime Text 2Traceback (most recent call last):
Now in sublime text 2, please input parameter:

  File
"D:\tmp\tmp_dev_root\python\tutorial_summary\sublime_text_2_ide\sublime_text_2_ide.py",
line 26, in <module>

    sublimeText2IdeDemo();

  File
"D:\tmp\tmp_dev_root\python\tutorial_summary\sublime_text_2_ide\sublime_text_2_ide.py",
line 22, in sublimeText2IdeDemo

    inputVal = raw_input("Now in sublime text 2, please input parameter:");

EOFError: EOF when reading a line

[Finished in 0.1s with exit code 1]

截图:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

【解决过程】

1.此处出错也很明显,就是Sublime中,运行python代码时,暂不支持输入参数,所以不支持Python中的input或raw_input,所处出现此错误。

2.所以接着就去想办法,添加对应的输入参数的支持。

3.参考:

【教程】把Sublime Text 2用作Python的IDE去实现Python的开发


Sublime Text 2 console input
提到的
SublimeREPL

4.参考其中提到的,先去:

http://wbond.net/sublime_packages/package_control

安装对应的包管理器。

快捷键:Ctrl+`,打开Sublime的console:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

Ctrl+V,粘贴下面代码:

import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print('Please restart Sublime Text to finish installation')

到console中,然后按Enter键,稍等一下,此包管理器即安装完成:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

然后重启sublime。

5.然后去

Preferences -> Package Control

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

选择Install Package

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

再选择sublimerepl:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

然后状态栏中就显示正在安装SublimeRepl:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

很快就安装完成了。

6.然后再去配置SublimeRepl:

Preferences  -> Package Settings  -> SublimeREPL -> Settings – User

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

暂时看到的配置是空的:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

暂时不需要添加,等后续需要再添加相应的配置。

7.参考,

http://sublimerepl.readthedocs.org/en/latest/

先确保当前打开的Python文件,然后再:

Tools -> SublimeREPL ->Python -> python

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

结果却发现,很悲催的是,其只是,另外单独打开了一个Python的Shell窗口:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

而不是我们所希望的,代替sublime的python。

所以,结果就是,根本没法通过SublimeREPL提供一个供Python代码输入参数的cmd环境。

8.再去自己折腾试试。

先试试:

Preferences  -> Browser Packages

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

然后可以找到很多语言的配置,包括了Python:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

可以看到对应的build的配置文件:

C:\Users\CLi\AppData\Roaming\Sublime Text 2\Packages\Python\Python.sublime-build

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

打开后,内容如下:

{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

可以看到,其是调用python,然后加上-u参数。

所以,先去cmd中,看看本身Python支持哪些参数:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\CLi>python -h

usage: python [option] … [-c cmd | -m mod | file | -] [arg] …

Options and arguments (and corresponding environment variables):

-B     : don’t write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x

-c cmd : program passed in as string (terminates option list)

-d     : debug output from parser; also PYTHONDEBUG=x

-E     : ignore PYTHON* environment variables (such as PYTHONPATH)

-h     : print this help message and exit (also –help)

-i     : inspect interactively after running script; forces a prompt even

if stdin does not appear to be a terminal; also PYTHONINSPECT=x

-m mod : run library module as a script (terminates option list)

-O     : optimize generated bytecode slightly; also PYTHONOPTIMIZE=x

-OO    : remove doc-strings in addition to the -O optimizations

-R     : use a pseudo-random salt to make hash() values of various types be

unpredictable between separate invocations of the interpreter, as

a defense against denial-of-service attacks

-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew

-s     : don’t add user site directory to sys.path; also PYTHONNOUSERSITE

-S     : don’t imply ‘import site’ on initialization

-t     : issue warnings about inconsistent tab usage (-tt: issue errors)

-u     : unbuffered binary stdout and stderr; also PYTHONUNBUFFERED=x

see man page for details on internal buffering relating to ‘-u’

-v     : verbose (trace import statements); also PYTHONVERBOSE=x

can be supplied multiple times to increase verbosity

-V     : print the Python version number and exit (also –version)

-W arg : warning control; arg is action:message:category:module:lineno

also PYTHONWARNINGS=arg

-x     : skip first line of source, allowing use of non-Unix forms of #!cmd

-3     : warn about Python 3.x incompatibilities that 2to3 cannot trivially fix

file   : program read from script file

-      : program read from stdin (default; interactive mode if a tty)

arg …: arguments passed to program in sys.argv[1:]

Other environment variables:

PYTHONSTARTUP: file executed on interactive startup (no default)

PYTHONPATH   : ‘;’-separated list of directories prefixed to the

default module search path.  The result is sys.path.

PYTHONHOME   : alternate <prefix> directory (or <prefix>;<exec_prefix>).

The default module search path uses <prefix>\lib.

PYTHONCASEOK : ignore case in ‘import’ statements (Windows).

PYTHONIOENCODING: Encoding[:errors] used for stdin/stdout/stderr.

PYTHONHASHSEED: if this variable is set to ‘random’, the effect is the same

as specifying the -R option: a random value is used to seed the hashes of

str, bytes and datetime objects.  It can also be set to an integer

in the range [0,4294967295] to get hash values with a predictable seed.

C:\Users\CLi>

所以,就去改为别的参数,去试试效果。

9.(1)改为:

{
"cmd": ["python", "-u -i", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

结果再去运行Python代码,结果是:

Unknown option: –
Unknown option: –

Unknown option: –

usage: python [option] … [-c cmd | -m mod | file | -] [arg] …

Try `python -h’ for more information.

[Finished in 0.1s with exit code 2]

(2)再改为:

{
"cmd": ["python", "-u", "$file", "-i'"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

结果是和之前同样的错误:EOFError: EOF when reading a line

(3)改为pythonw:

{
"cmd": ["pythonw", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

结果是,没变化,觉得有问题。所以重启sublime,然后再运行,结果是类似的错误

Traceback (most recent call last):Demo print in Sublime Text 2
  File
"D:\tmp\tmp_dev_root\python\tutorial_summary\sublime_text_2_ide\sublime_text_2_ide.py",
line 26, in <module>

Now in sublime text 2, please input parameter:    sublimeText2IdeDemo();

  File
"D:\tmp\tmp_dev_root\python\tutorial_summary\sublime_text_2_ide\sublime_text_2_ide.py",
line 22, in sublimeText2IdeDemo

inputVal = raw_input("Now in sublime text 2, please input parameter:");

EOFError: EOF when reading a line

[Finished in 0.8s with exit code 1]

10.实在没辙了。

只能找别的办法了。

参考:

Python 3.1 and Sublime Text 2 error

知道了,官网已经有人讨论了此问题了:

Strange is this a bug?

但是还是官网没有真正解决。

还是这人弄了个插件支持输入:

https://github.com/eric-wieser/build-with-input

但是不能同时支持输出和输入,这个很恼人,所以也懒得去试了。

11.最后,是参考:

Sublime Text 2最为python开发的ide还不错

(先去把Python配置恢复为最原始的:

{
"cmd": ["python", "-u", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}

去:

(1)打开当前python文件

(2)然后

Tools -> Command Palette

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

(3)SublimeREPL Python RUN current file

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

(4)就会打开新窗口,显示python的shell,可以供你输入参数了:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

(5)接着就可以输入参数,再按Enter,即可输入:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

如上的运行当前Python文件,也可以通过:

Tools -> SublimeREPL -> Python -> RUN current file

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

12.另外,关于窗口显示,设置为上下显示,即2 row的形式,更方便我们调试代码:

View -> Layout -> Rows: 2

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

然后再去运行:

Tools -> SublimeREPL -> Python -> RUN current file

好像还是默认同行显示:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

但是我们可以手动拖动到下面那栏:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

就可以上下显示了:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

然后接着就可以正常调试了:

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

13.另外,又试了试,

第二次,再去

Tools -> SublimeREPL -> Python -> RUN current file

运行Python代码时,结果又出现其他错误:

D:\tmp\dev_install_root\Python27_x64\python.EXE: can’t open file ‘$file_basename’: [Errno 2] No such file or directory

***Repl Closed***

已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line(转)

所以,真心的,不好用。

【总结】

对于别的很多IDE都集成很好的Python的shell环境,Sublime竟然不支持,而且即使使用上述SublimeREPL去间接支持,都还是很不好用。

还不能同时支持输入和输出,所以,用Sublime来开始Python的话,真的不是好的选择。

还是用

【整理】各种Python的IDE(集成开发环境)的总结和对比

中提到的,各种其他IDE,比如有Eclipse+PyDev,Ulipad等等,比较好用。

当然,Sublime的一个比较大的优点是,界面好看。

上一篇:javascript ES3小测试


下一篇:关于snappy的压缩 hadoop和hbase均支持