-6

I am new to Python, have some experience in MatLab and r. My question is: Is it possible to run part of the code in .py block by block (line by line)?

In r or Matlab, I can first have some data and variables loaded in the memory first. Then experimentally I can run a line or two to try out the syntax... this is particularly useful for new learners I believe. I know there is something called the iPython which can execute Python code line by line however this is not what I am after. Thanks.

Lafayette
  • 367
  • 1
  • 5
  • 12
  • https://docs.python.org/2/library/pdb.html – SiHa Feb 19 '16 at 08:33
  • Have a look at [Python Drop into REPL (Read, Eval, Print, Loop)](https://stackoverflow.com/questions/1395913/python-drop-into-repl-read-eval-print-loop) – sloth Feb 19 '16 at 08:34
  • 1
    ipython-notebook is good for interactive computing. It runs in browser and allows you to do similar things you can do in R-Studio or Octave . – Nikolay Dudaev Feb 19 '16 at 08:38
  • Why is Ipython not the right tool for your request? It does exactly what you want. – Serbitar Feb 19 '16 at 08:45
  • @Serbitar: yes, I think iPython pretty much can do what I want. However, I am trying to see if there is an coding environment in Python which would be similar to r Studio. I am using pyCharm now. – Lafayette Feb 19 '16 at 10:05

2 Answers2

3

Since ipython has already been discounted, I'm not sure this answer will be better. But I will tell you the two things that I do.

  1. I drop into the debugger at the point where I want to "try out" something, so the code will run up to that point, and then drop me into the debugger. You do this simply by inserting this code at that point:

    import pdb; pdb.set_trace()
    

    Once you've done what needs to be done, you can either press q to quit, or c to continue running the process.

  2. I use the -i option to python. This enters interactive mode at the end of your python code. This is useful if you want to set up a bunch of data structures, and try out some code on it, instead of typing all of it into a python shell first. (that might be why you rejected ipython?)

izak
  • 981
  • 7
  • 10
0

I think what you need is a debugger.

You can use the pydev plugin for Eclipse which has a debugger.

Another option is pdb as already suggested but it's not very easy to use.

Ionut Ticus
  • 2,683
  • 2
  • 17
  • 25