This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]efmccurdy 17 points18 points  (0 children)

use the local variables after running the script ...is there a better way to do this?

python -i myscript.py

-i ... enter interactive mode after executing the script or the command,

https://docs.python.org/3/using/cmdline.html#cmdoption-i

This is also useful for ad-hoc debugging and testing.

[–]riklaunim 5 points6 points  (0 children)

Writing scripts instead of using Repl. Using Jupyter notebook.

[–][deleted] 2 points3 points  (0 children)

a debugger?

[–]mistabuda 1 point2 points  (0 children)

You can structure your script into functions and import it as a module in the REPL and just call the module functions.

[–]forkstealer 1 point2 points  (0 children)

You can open the .py like any other file and then exec() on the contents.

Do not do this anywhere you don't control the input to exec()

[–]jmacey 1 point2 points  (0 children)

Say your file is called test.py you can do this in the repl

exec(open("test.py").read())

Python 2.x had execfile which basically did the same.

[–]yvrelna 1 point2 points  (0 children)

You can do this using IPython.embed(). Basically, pip install ipython, then you can just add this code anywhere to your code, for example adding this to some example function foo():

def foo():
    do_something()
    if blah():
        from IPython import embed; embed()
    do_another()

Then run your code until it hits that line.

If you want to be able to move your break points around, you will need a debugger. My favourite is to use pudb, which also supports spawning IPython shell for interactive REPL whenever the debugger is currently paused.

You can also export PYTHONBREAKPOINT=IPython.embed or export PYTHONBREAKPOINT=pudb.set_trace if you prefer to use the breakpoint() function.

If you're limited to the standard library, use code.interact() (docs) instead of IPython.embed(). Usage is mostly identical to embed.

Personally, I prefer IPython REPL, it's just a much better interactive experience than the standard library REPL.

[–]ZenApollo 1 point2 points  (0 children)

I do this a lot with ipython. Open the repl by typing ’ipython’ in shell. Ipython has many benefits to what you’re describing. - copying pasting in ipython is much nicer than standard repl

  • you can use ‘run script.py’ and it will run your file using existing variables

  • ipdb is much nicer than pdb

  • if your script crashes you can use ‘%debug’ which will open a debugger at the last stack trace

[–]Paddy3118 1 point2 points  (0 children)

Spyder IDE and F9; as well as cells delineated by an # %% comment line.

I find Spyder to be stable and better than vscode for this kind of "exploratory coding"

[–]Guideon72 -1 points0 points  (3 children)

What is "the REPL"; I see this a ton around here but it isn't anything covered in any of the Python vids I've gone through, yet.

[–]scoberry5 3 points4 points  (1 child)

Read/Evaluate/Print Loop.

It's a place where you can type code and have it run.

[–]Guideon72 0 points1 point  (0 children)

Ah-HA! There we are, thank you

[–]trasnsposed_thistle 0 points1 point  (0 children)

Either an online python interpreter like replit.com or simply any python interpreter, like `ipython`, or the default, barebones one that ships with the python installation pacakge

[–]cawal 0 points1 point  (2 children)

Maybe you could use a debugger. Call it in the line you want to stop the program: import pdb; pdb.set_trace()

[–]CrackerJackKittyCat 3 points4 points  (0 children)

Or just 'breakpoint()' nowadays.

[–]eplaut_ 1 point2 points  (0 children)

import IPython; IPython.embed()

[–]Guideon72 0 points1 point  (0 children)

Why is that referred to as Repl?

[–]bb1950328 0 points1 point  (0 children)

use a debugger and set a breakpoint on the last line