【問題】
在折騰:
【已解決】Sublime Text 2中運行Python程序出錯:The system cannot find the file specified
的過程中,雖然解決了找不到python.exe的問題,但是又出現下面這個錯誤:
Demo print in Sublime Text 2Traceback (most recent call last): |
截圖:
【解決過程】
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+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。
5.然后去
Preferences -> Package Control
選擇Install Package
再選擇sublimerepl:
然后狀態欄中就顯示正在安裝SublimeRepl:
很快就安裝完成了。
6.然后再去配置SublimeRepl:
Preferences -> Package Settings -> SublimeREPL -> Settings – User
暫時看到的配置是空的:
暫時不需要添加,等后續需要再添加相應的配置。
7.參考,
http://sublimerepl.readthedocs.org/en/latest/
先確保當前打開的Python文件,然后再:
Tools -> SublimeREPL ->Python -> python
結果卻發現,很悲催的是,其只是,另外單獨打開了一個Python的Shell窗口:
而不是我們所希望的,代替sublime的python。
所以,結果就是,根本沒法通過SublimeREPL提供一個供Python代碼輸入參數的cmd環境。
8.再去自己折騰試試。
先試試:
Preferences -> Browser Packages
然后可以找到很多語言的配置,包括了Python:
可以看到對應的build的配置文件:
C:\Users\CLi\AppData\Roaming\Sublime Text 2\Packages\Python\Python.sublime-build
打開后,內容如下:
{ "cmd": ["python", "-u", "$file"], "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", "selector": "source.python" }
可以看到,其是調用python,然后加上-u參數。
所以,先去cmd中,看看本身Python支持哪些參數:
Microsoft Windows [Version 6.1.7601] C:\Users\CLi>python -h 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: 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: – 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 Now in sublime text 2, please input parameter: 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
知道了,官網已經有人討論了此問題了:
但是還是官網沒有真正解決。
還是這人弄了個插件支持輸入:
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
(3)SublimeREPL Python RUN current file
(4)就會打開新窗口,顯示python的shell,可以供你輸入參數了:
(5)接着就可以輸入參數,再按Enter,即可輸入:
如上的運行當前Python文件,也可以通過:
Tools -> SublimeREPL -> Python -> RUN current file
12.另外,關於窗口顯示,設置為上下顯示,即2 row的形式,更方便我們調試代碼:
View -> Layout -> Rows: 2
然后再去運行:
Tools -> SublimeREPL -> Python -> RUN current file
好像還是默認同行顯示:
但是我們可以手動拖動到下面那欄:
就可以上下顯示了:
然后接着就可以正常調試了:
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 |
所以,真心的,不好用。
【總結】
對於別的很多IDE都集成很好的Python的shell環境,Sublime竟然不支持,而且即使使用上述SublimeREPL去間接支持,都還是很不好用。
還不能同時支持輸入和輸出,所以,用Sublime來開始Python的話,真的不是好的選擇。
還是用
【整理】各種Python的IDE(集成開發環境)的總結和對比
中提到的,各種其他IDE,比如有Eclipse+PyDev,Ulipad等等,比較好用。
當然,Sublime的一個比較大的優點是,界面好看。