如何在 terminal 运行 python 脚本,显示出 interactive 的结果? - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yangzh

如何在 terminal 运行 python 脚本,显示出 interactive 的结果?

  •  
  •   yangzh Dec 18, 2013 7729 views
    This topic created in 4528 days ago, the information mentioned may be changed or developed.
    比如说,我有这么一个 test.py:
    ```
    print 'hello world!'
    1+1
    a=3
    a
    ```

    如果我在 terminal 运行的话:
    ```
    $ python test.py
    hello world!
    ```

    我想达成这么一个效果,在 terminal 运行:
    ```
    $ magicpython test.py
    >>> print 'hello world!'
    hello world!
    >>> 1+1
    2
    >>> a=3
    >>> a
    3
    ```

    我想知道的就是 magicpython 这个程序怎样写。原生的 python 和 ipython 都没有这个效果。
    8 replies    1970-01-01 08:00:00 +08:00
    felix021
        1
    felix021  
       Dec 18, 2013   1
    yangzh
        2
    yangzh  
    OP
       Dec 18, 2013
    @felix021 这位仁兄亲自操刀一个。真是感动。虽然我以为会有什么库是支持这个需求的。
    felix021
        3
    felix021  
       Dec 18, 2013
    @yangzh 纯粹闲的蛋疼。。本来想用pty.spawn来搞的,但是退出不了,所以还是用compiler自己搞。
    polythene
        4
    polythene  
       Dec 18, 2013   2
    @yangzh 如果你调试过python程序,就应该知道pdb这个库正巧满足你的需求
    python -m pdb test.py
    xiaket
        5
    xiaket  
       Dec 19, 2013   1
    import code
    code.interact(local=locals())

    代码太短懒得贴gist了, python脚本里这两句就够了.
    yangzh
        6
    yangzh  
    OP
       Dec 19, 2013
    @polythene 这么便捷?!谢谢哈!我不是专业的,只是写了一些段程序,用土办法 print 来调。

    @xiaket 我回家试一下!
    yangzh
        7
    yangzh  
    OP
       Dec 19, 2013
    @polythene 呃,我试了一下,其实和我显示的效果相差挺大的。。。虽然原理应该差不多。
    mckelvin
        8
    mckelvin  
       Jan 18, 2014 via Android
    先开python或ipython,再execfile('test.py')
    About     Help     Advertise     Blog     API     FAQ     Solana     1421 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 51ms UTC 17:05 PVG 01:05 LAX 10:05 JFK 13:05
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86