Run only certain lines of code

A forum for general discussion of the Python programming language.

Run only certain lines of code

Postby ppaarrkk » Fri May 17, 2013 11:21 am

Hello.

I am new to python. I use R. I'm using PyScripter for python. I can run a whole script, but often I just want to run one line or a small block. In R, I can just copy and paste directly into the session. Is there a way to do this in python? If I just copy and paste, then it doesn't recognize them as commands.


Thanks.
ppaarrkk
 
Posts: 5
Joined: Fri May 17, 2013 11:15 am

Re: Run only certain lines of code

Postby setrofim » Fri May 17, 2013 1:50 pm

Python has an interactive console (similar to R's) which you can invoke py just typing "python" in your terminal (with no arguments). It's pretty basic compared to R's (you can get a more powerful console by installing ipython). You can paste the code into the interactive console to run individual lines (provided the right context exists, e.g. you've imported the required modules, etc). You may also look into ipython notebooks (installed with ipython).
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Run only certain lines of code

Postby ppaarrkk » Tue May 21, 2013 10:32 am

Thanks. That works. It's quite basic. What do most people do if they just want to run selected lines of code?

If you just want to run these two lines, for example:

string2 = "b"
print string2

If you paste them into the standard IDLE editor, nothing happens because it doesn't see the second statement. I agree that it works in the basic python editor that comes with python, but surely this is something that people do ? Python users must at times only run certain lines of code ?

Thanks.
ppaarrkk
 
Posts: 5
Joined: Fri May 17, 2013 11:15 am

Re: Run only certain lines of code

Postby ppaarrkk » Tue May 21, 2013 10:56 am

Ok, I've found it in PyScripter. It's Ctrl+F7.
ppaarrkk
 
Posts: 5
Joined: Fri May 17, 2013 11:15 am

Re: Run only certain lines of code

Postby ppaarrkk » Tue May 21, 2013 11:01 am

....and you can change key bindings.

http://code.google.com/p/pyscripter/iss ... ail?id=307
ppaarrkk
 
Posts: 5
Joined: Fri May 17, 2013 11:15 am

Re: Run only certain lines of code

Postby setrofim » Tue May 21, 2013 11:57 am

ppaarrkk wrote:If you paste them into the standard IDLE editor, nothing happens because it doesn't see the second statement. I agree that it works in the basic python editor that comes with python, but surely this is something that people do ? Python users must at times only run certain lines of code ?

Python typically isn't used the same way as R -- people don't spend a lot of time in the interactive interpreter (usually it's just to try things out). If you are looking for a more advanced interactive experience, look into ipython and ipython notebooks.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Run only certain lines of code

Postby Kebap » Tue May 21, 2013 12:06 pm

ppaarrkk wrote:What do most people do if they just want to run selected lines of code?

Depends on your Python editor of choice. Most have an interactive shell. Then just type the line there, hit Enter, see it process immediately. If it gets more complicated, you may capsule it in a function which you can call. Or you indeed go into the editor and do something like "run single line" or "run until here" or what you need. I use the interactive shell a lot when I am prototyping and debugging my code.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 377
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests