IDLE Editor - Running just a selection of the code

Hi, I am new to Python programming. I have done a lot SAS, SQL, R etc.

In most of the coding environments I have used I can simply mark/select a chunk of code and run just that chunk.

Is this possible in the basic IDLE editor that comes with Python.

Thanks

Andrew

Hello,

I don’t think that the IDLE editor provides this feature. Spyder does, however (there may be others)

PyCharm (community edition - free) has the option of using scratch modules where you can run test scripts without creating full blown projects, is an alternative option as well.

Hi Paul, Thanks for the response.
The organisation I work for is limited in what it can/will allow to be installed.
Spyder would probably be my choice but to get it installed might not be a runnner.
I do currently have Visual Studio 2022 which is proving beyond me to set up. That’s why I thought IDLE might be usable.
Cheers
Andrew

Hi,

if you have to use the IDLE editor, you may comment out the parts that you don’t want to run. If you’re on Windows, you can select the parts of the script that you want to comment out and press alt + 3. To uncomment out, press alt + 4.

Give PyCharm a try. Fairly easy.

Thanks Paul.

As an IDLE maintainer, I have seen such requests before and have thought a bit about it. The main issue is that running from the editor is intended to be like running from a file with the -i option. But python has no option to run part of a file. So the code run must match a file. So the selection would have to be written to a scratch file, or run as if entered into the Shell, which only allows a single statement, or a new mode has to be invented. Another is that traceback line numbers would not match editor line numbers.

Does anyone know what editors with this feature do? Do selections have to be complete lines?

I am also curious what the use case is? Most selections would require prior code to be run. Is the use case ever to run the first n lines of a file?

Currently one can select, copy, and paste into Shell. Would a single command to do all 3 do what is wanted?

There does not seem to be a current IDLE issue for this so I created draft issue “Run a selection of code in the editor” at IDLE Issues · GitHub.

Hi All, If its any use to anyone as a suggestions, a couple of people have recommended VS Code. I understand it’s a Microsoft product and is a kind of cut down version of Visual Studio. Thanks for all your help.
Andrew

Spyder cells use some kind of IPython magic.

Yes.

I personally use cells for two distinct cases:

  1. Code organization. Sometimes I have groups of related functions which do not belong to a class. Then I use cells to organize them:
  2. As “poor man’s functions” in quick-and-dirty scripts, typically for processing of one-off data. I might have a big file containing some kind of data, which I want to load and do something with. Then I might put the file loading in one cell, and the processing in one or more other cells. Then I can easily modify and rerun the processing logic without reloading the data.
    That might seem lazy, but if it’s a data format I never need to touch again (happens surprisingly often), writing a more robust parser is overkill.

but you get a lot back if you hang on (spread the learning over time) :wink:

Thanks for the information; it has given me more to think about. IDLE is restricted to stdlib imports and hence cannot use “IPython magic and its cells”. However, it does have a module browser that represents a file as a currently static tree of class and function definitions. I have thought of enhancing it by live-linking it to the file for moving chunks of code around. (Your case 1.) Using it to run part of a file is a new idea. IDLE can already run a whole file without restarting the Shell session. So a restricted version of your case 2 should be possible.