From: Brendan Miller on
I have a python script that sets up some environmental stuff. I would
then like to be able to change back to interactive mode and use that
environment. What's the best way to do that?
From: Brendan Miller on
python -i myscript.py

almost does what I want. The only problem is if I exit with exit(0) it
does *not* enter interactive mode. I have to run off the end of the
script as near as I can tell. Is there another way to exit without
breaking python -i?

On Thu, May 20, 2010 at 4:57 PM, Brendan Miller <catphive(a)catphive.net> wrote:
> I have a python script that sets up some environmental stuff. I would
> then like to be able to change back to interactive mode and use that
> environment. What's the best way to do that?
>
From: Steven D'Aprano on
On Thu, 20 May 2010 16:57:40 -0700, Brendan Miller wrote:

> I have a python script that sets up some environmental stuff. I would
> then like to be able to change back to interactive mode and use that
> environment. What's the best way to do that?

On most(?) Linux distros, `man python` is your friend. (Like all well-
behaviour apps, python should come with a man page.) `python --help` is
useful too, and will be platform independent. Both tell me that you want
to pass -i as an option to enter the interactive interpreter after
running a module or command:

[steve(a)sylar ~]$ python -c "print 'hello world'; x = 2"
hello world
[steve(a)sylar ~]$ python -i -c "print 'hello world'; x = 2"
hello world
>>> x
2

You can also set an environment variable to force the same behaviour. See
the help for details.


--
Steven
From: Patrick Maupin on
On May 20, 6:57 pm, Brendan Miller <catph...(a)catphive.net> wrote:
> I have a python script that sets up some environmental stuff. I would
> then like to be able to change back to interactive mode and use that
> environment. What's the best way to do that?

>>> import cmd
>>> class MyCmd(cmd.Cmd):
.... def default(self, line):
.... exec line in globals()
....
>>> MyCmd().cmdloop()
(Cmd) s = 'The answer is probably to use %s'
(Cmd) print s % 'the cmd module'
The answer is probably to use the cmd module
(Cmd)

Regards,
Pat
From: Steven D'Aprano on
On Thu, 20 May 2010 17:11:17 -0700, Brendan Miller wrote:

> python -i myscript.py
>
> almost does what I want. The only problem is if I exit with exit(0) it
> does *not* enter interactive mode. I have to run off the end of the
> script as near as I can tell. Is there another way to exit without
> breaking python -i?

Upgrade to Python 2.6 and it will work as expected.

[steve(a)sylar ~]$ python2.6 -i -c "import sys; sys.exit(0)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
SystemExit: 0
>>>



If you're stuck with 2.5, you can wrap your script in a try...except and
catch the exit:


[steve(a)sylar ~]$ python -i -c "import sys
> try:
> x = 2
> sys.exit(0)
> except SystemExit:
> pass
> "
>>> x
2


The only side-effect I can think of is that you may lose any non-zero
result codes, but you probably do that after entering interactive mode
anyway.

If you want to be fancy, you can inspect the state of the environment
variable and Python options inside the except block, and re-raise if
necessary. Untested:

except SystemExit:
if os.getenv('PYTHONINSPECT'):
pass
else:
raise


I can't find a way to check for a -i switch from inside Python 2.5. There
is a sys.flags but it is only available in Python 2.6 or higher.



--
Steven