Python - Running just a single line of code, and not the rest of the Multiple lines in the Script
Asked Answered
L

5

6

In - Python, inside the the IDLE, in the File Editor Window,

How do you run just a selected single line of code in the script, without having the rest of the lines of the program being runned ?

Loleta answered 10/9, 2015 at 13:32 Comment(0)
S
15

You'll have to run your line of code form command line:

With the -c (command) argument (assuming your file is named foo.py):

$ python -c 'import foo; print foo.hello()'
Snooker answered 10/9, 2015 at 13:42 Comment(2)
I have tried inputting what typed above in the file editor window. but what gets produced in the Shell, is $ python -c . My file name is Traffic Calculation.py , here is the line i just want to run, print('Hi John, what road are you travelling for work today ?') so how this be written ?Loleta
You need to input this in the shell comnand line not in editorSnooker
M
1

You can use the IdleX extension (https://github.com/serwy/idlex): it adds the ability to run any selected text by pressing F9 (among other things that I haven't tested yet).

Mae answered 15/7, 2023 at 2:26 Comment(0)
D
0

You can select and copy a single statement in an Idle editor (or anywhere else, for that matter), switch to the Idle Shell, and paste on the line with the >>> prompt as the bottom. (You can hit to get a clean prompt.) Then hit return just as it you had entered into the shell directly. This works for multiline statements. Being able to do this with a menu selection, hotkey, or right-click selection is on my todo list, as you are not the first to ask about this.

Dimissory answered 11/9, 2015 at 7:24 Comment(0)
D
0

Basically, what we call "debugger" should be fulfilled your requirement. Many IDEs are provided "debugger tools" including IDLE. To use a debugger tool you can just simply

  1. add break lines (any lines of code you want a program to stop while program is running),
  2. explore your variables (when program is stop you can print out values of your variables at that time)
  3. Click/type step to go to the next breakpoint.

This is just a roughly procedure that should be fit to your requirement.

see more: debug

Druid answered 11/9, 2015 at 7:46 Comment(0)
I
0

Use a debugger tool. Multiple IDE's provide these within the application or website. You are able to add break lines, which means you are able to stop certain lines of code while the rest of the code runs.

Interlock answered 7/10 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.