最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Python IDE crifan 24099浏览 0评论

【问题】

在折腾:

【已解决】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]

截图:

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:

ctrl shortcut show sublime console

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键,稍等一下,此包管理器即安装完成:

download and install related package ok

然后重启sublime。

5.然后去

Preferences -> Package Control

sublime preferences package control

选择Install Package

choose install package

再选择sublimerepl:

choose sublimerepl

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

status bar show installing sublimerepl

很快就安装完成了。

6.然后再去配置SublimeRepl:

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

Preferences  Package Settings  SublimeREPL  Settings - User

暂时看到的配置是空的:

settings is empty

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

7.参考,

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

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

Tools -> SublimeREPL ->Python -> python

tools SublimeREPL Python python

 

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

just new window to run python shell

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

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

8.再去自己折腾试试。

先试试:

Preferences  -> Browser Packages

Preferences  Browser Packages

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

Sublime Text 2 Packages Python


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

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

found Python sublime build

打开后,内容如下:

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

python.sublime-build content

可以看到,其是调用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

Tools  Command Palette

(3)SublimeREPL Python RUN current file

SublimeREPL Python RUN current file

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

show shell allow input

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

can accept inputed para

 

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

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

Tools SublimeREPL Python run current file

 

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

View -> Layout -> Rows: 2

View Layout Rows 2

然后再去运行:

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

好像还是默认同行显示:

new windows still in same line

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

drag from above to bottom line

就可以上下显示了:

has draged to bottom line

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

normal debug in show 2 row

 

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***

python.EXE cant open file file_basename Errno 2 No such file or directory

所以,真心的,不好用。

 

【总结】

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

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

还是用

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

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

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

转载请注明:在路上 » 【已解决】Sublime中运行带input或raw_input的Python代码出错:EOFError: EOF when reading a line

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (11)

  1. sorry 没看清
    路过6年前 (2018-03-24)回复
  2. 怎么说呢。跟着你的步骤做了下来,你却的最后说这个方法不能用 太浪费时间了,为什么不在开头就说失败了呢。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    路过6年前 (2018-03-24)回复
  3. 最暴力的方法: sublime命令行执行import os;os.system("python XXX.py")
    JING10年前 (2014-09-13)回复
  4. 感谢楼主分享经历 好消息是问题已解决~ http://stackoverflow.com/questions/14080041/running-python-interactively-from-within-sublime-text-2 user keybindings加入 { "keys": ["super+shift+r"], "caption": "Python", "command": "repl_open", "mnemonic": "p", "args": { "type": "subprocess", "encoding": "utf8", "cmd": ["python", "-i", "-u", "$file"], "cwd": "$file_path", "syntax": "Packages/Python/Python.tmLanguage", "external_id": "python" } }
    罗帆10年前 (2014-03-22)回复
    • 复制的不好,是第二个答案 http://stackoverflow.com/a/14080562
      罗帆10年前 (2014-03-22)回复
      • 真的不错,学习了
        Victor10年前 (2014-10-18)回复
        • 这个答案不错,可以在解释器运行结果后,再次进行解释器输入。但是呢,我如果定义一个函数,照理来说,解释器应该要是可以重复调用这个函数的啊?比如: def factorial(number): product = 1 for i in range(number): product = product * (i+1) return product user_input = input("Enter a non-nagative interger to take the factorial of: ") factorial_of_user_input = factorial(user_input) print factorial_of_user_input 我这是个阶乘函数,我用了两位前辈最终的解决方案,F5运行后,输入3,得结果6,这时候弹出“>>>”,可以再次输入,我就输入4,得出结果4,并没有重复调用我设置的函数,这个怎么解决呢? 网络上探讨这样的问题的朋友真的不多啊,盼交流。
          Leon9年前 (2014-12-29)回复
          • 这个问题是解决了,是我弄错了,我再次调用应该是码出来factorial(4),这个问题解决了sublime很好用了!真实大感激!
            Leon9年前 (2014-12-30)回复
          • 没去研究过这样的问题。话说,还是建议用notepad++加上cmd去开发Python吧。。。就可以避免这样的诡异问题了。
            crifan9年前 (2015-01-01)回复
101 queries in 0.183 seconds, using 22.22MB memory