如何在 terminal 运行 python 脚本,显示出 interactive 的结果? - V2EX
首页
注册
登录
V2EX = way to explore
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
V2EX
Python
如何在 terminal 运行 python 脚本,显示出 interactive 的结果?
yangzh
2013 年 12 月 18 日
7615 次点击
这是一个创建于 4444 天前的主题,其中的信息可能已经有所发展或是发生改变。
比如说,我有这么一个 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 都没有这个效果。
World
hello
test
8 条回复
1970-01-01 08:00:00 +08:00
1
felix021
2013 年 12 月 18 日
1
https://gist.github.com/felix021/8021106
2
yangzh
OP
2013 年 12 月 18 日
@
felix021
这位仁兄亲自操刀一个。真是感动。虽然我以为会有什么库是支持这个需求的。
3
felix021
2013 年 12 月 18 日
@
yangzh
纯粹闲的蛋疼。。本来想用pty.spawn来搞的,但是退出不了,所以还是用compiler自己搞。
4
polythene
2013 年 12 月 18 日
2
@
yangzh
如果你调试过python程序,就应该知道pdb这个库正巧满足你的需求
python -m pdb
test.py
5
xiaket
2013 年 12 月 19 日
1
import code
code.interact(local=locals())
代码太短懒得贴gist了, python脚本里这两句就够了.
6
yangzh
OP
2013 年 12 月 19 日
@
polythene
这么便捷?!谢谢哈!我不是专业的,只是写了一些段程序,用土办法 print 来调试。
@
xiaket
我回家试一下!
7
yangzh
OP
2013 年 12 月 19 日
@
polythene
呃,我试了一下,其实和我显示的效果相差挺大的。。。虽然原理应该差不多。
8
mckelvin
2014 年 1 月 18 日 via Android
先开python或ipython,再execfile('
test.py
')
关于
帮助文档
自助推广系统
博客
API
FAQ
Solana
1868 人在线
最高记录 6679
Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 23ms
UTC 02:57
PVG 10:57
LAX 18:57
JFK 21:57
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