python -i(交互式环境) [英] python -i (interactive environment)

查看:152
本文介绍了python -i(交互式环境)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您运行python -i scriptname.py时在脚本完成后,您在交互式命令提示符处离开了




有没有办法从正在运行的程序中发生这种情况?


换句话说,我可以运行scriptname.py(不是python -i scriptname.py)

和scriptname.py里面我决定我要回到

交互式提示?


我已经搜索过,到目前为止,我唯一想到的就是使用pdb,但是

与交互式提示不完全相同。


有没有办法做到我错过了?


谢谢。

When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I''ve searched and so far the only thing I''ve come up with is to use pdb, but
that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.

推荐答案

很简单就是你在UNIX上...


你juste必须放在你文件的开头:


#!/ usr / bin / python -i


它和juste做了什么你想要:)


Pierre


Joeaécrit:
Very simple is you''re on UNIX ...

You juste have to put at the beginnin of your file :

#!/usr/bin/python -i

And it juste does what you want :)

Pierre

Joe a écrit :
当你运行python -i时scriptname.py"在脚本完成后,您将在交互式命令提示符处离开

有没有办法从正在运行的程序中发生这种情况?

换句话说我可以只需运行scriptname.py(不是python -i scriptname.py)
并在scriptname.py中我决定回到
交互式提示符吗?

我我已经搜索过了,到目前为止,我唯一想到的就是使用pdb,但
与交互式提示不完全相同。

有什么办法吗?这样做我错过了吗?

谢谢。
When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I''ve searched and so far the only thing I''ve come up with is to use pdb, but
that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.



嗨皮埃尔,


感谢您的回复,但我不是在Unix上,即使我是那个

解决方案,它也达不到预期的效果。


我希望脚本决定是否回退到交互式提示符。

您的解决方案使其始终回退到交互式提示符。


我想做点什么ike:


试试:

#执行代码

除了MyExceptionOccurred,except_msg:

#fall交互式提示。


换句话说,我想让我的程序确定是否显示

交互式提示。


谢谢!

" Pierre Barbier de Reuille" < PI ************ @ cirad.fr>在消息中写道

news:42 *********************** @ news.free.fr ...
Hi Pierre,

Thanks for the reply, but I am not on Unix and it even if I was that
solution it does not achieve the desired results.

I want the script to decide whether to fall back to the interactive prompt.
You solution makes it ALWAYS fall back to the interactive prompt.

I want to do something like:

try:
# execute code
except MyExceptionOccurred, except_msg:
# fall to interactive prompt.

In other words I want to have my program to determine whether the
interactive prompt to be displayed or not.

Thanks!
"Pierre Barbier de Reuille" <pi************@cirad.fr> wrote in message
news:42***********************@news.free.fr...
很简单就是你在UNIX上...

你必须把你的文件放在你的文件的开头:

#!/ usr / bin / python -i

而且它正是你想做的事情。

皮埃尔

Joeaécrit:
Very simple is you''re on UNIX ...

You juste have to put at the beginnin of your file :

#!/usr/bin/python -i

And it juste does what you want :)

Pierre

Joe a écrit :
当你运行python -i scriptname.py时在脚本完成之后你将离开交互式命令提示符。

有没有办法从正在运行的程序中发生这种情况?

换句话说我可以只需运行scriptname.py(不是python -i scriptname.py)
并在scriptname.py中我决定回到
交互式提示符吗?

我我已经搜索过,到目前为止我唯一想到的就是使用pdb,
但这与交互式提示不完全相同。

有什么办法吗?这样做我错过了吗?

谢谢。
When you run "python -i scriptname.py" after the script completes you
left at the interactive command prompt.

Is there a way to have this occur from a running program?

In other words can I just run scriptname.py (NOT python -i scriptname.py)
and inside of scriptname.py I decide that I want to fall back to the
interactive prompt?

I''ve searched and so far the only thing I''ve come up with is to use pdb,
but that is not exactly the same as the interactive prompt.

Is there any way to do it that I have missed?

Thanks.



Joe写道:
Joe wrote:
我希望脚本决定是否回退到交互式提示。
你的解决方案使它总是回到交互式提示。
I want the script to decide whether to fall back to the interactive prompt.
You solution makes it ALWAYS fall back to the interactive prompt.




实际上,使用sys.exit()意味着即使使用了python -i

,程序也可以退出。


你可以使用:


导入代码

code.interact()


模拟交互式提示。

-

Michael Hoffman



Actually, using sys.exit() means the program can exit even if python -i
is used.

You can use:

import code
code.interact()

which emulates the interactive prompt.
--
Michael Hoffman


这篇关于python -i(交互式环境)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆