The title maybe a little bit confusing, I will explain it.

I am using opensuse. Usually I work in a shell like this:
$vim
$python
>>>running...
>>>running...
>>>running...
>>>running...
$

Here the program end, but I cannot make further change nor access to the variable. I know I can do this:
$python
>>>
then paste the python code on it and it will run

But it is quite troublesome to open 2 shell and copy and paste...
Can anyone suggest a better method?

Thanks

Dani AI

Generated

As noted, Python supports an interactive run mode so the interpreter stays open after the script finishes (see the command-line docs). That is the simplest way to avoid the edit/run/copy-paste loop that described.

Other useful workflows and tricks:

  • Import/reload loop: work in a REPL, import the script as a module, call its functions, then use importlib.reload after edits to pick up changes without restarting the interpreter. Example:

    import test
    from importlib import reload
    reload(test)

    Wrap runnable code in an if __name__ == "__main__": block so importing does not execute side effects. See importlib.reload docs.

  • Break into a debugger or embed a REPL: add a breakpoint with import pdb; pdb.set_trace() or run under the debugger (python -m pdb) to inspect locals inside functions (pdb docs). To drop into a full interactive prompt at a chosen point, use the code module:

    import code
    code.interact(local=globals())

    (see code.interact docs).

  • Use IPython for a richer REPL: its %run -i magic runs a script and leaves variables in the interactive namespace, with better history and completion (IPython %run docs).

Notes and cautions: interactive entry won’t occur if the script raises SystemExit or an uncaught exception; variables local to a finished function are not visible at module level (return them or assign to module globals to inspect). For fast iteration, keep logic in small functions and rely on import/reload or an embedded REPL rather than copying code between shells.

Recommended Answers

All 2 Replies

You should run

$ python -i test.py

It runs your program, then starts the interactive python shell. You should also have a look at python -h.

Thanks!!
Really thanks!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.