Coding in Interactive Mode vs Script Mode

This post was originally published here

When programming in Python, you have two basic options for running code: interactive mode and script mode. Distinguishing between these modes can be slightly confusing for beginners, especially when you’re trying to follow along with others’ tutorials, so here’s a brief rundown.

Interactive Mode

Interactive mode is great for quickly and conveniently running single lines or blocks of code. Here’s an example using the python shell that comes with a basic python installation. The “>>>” indicates that the shell is ready to accept interactive commands. So for example if you want to print the statement “this is interactive mode”, simply type the appropriate code and hit enter.

interactive mode example

If by chance you instead have the Spyder code editor installed, printing the same statement in interactive mode will look something like this:

interactive mode spyder example

The IPython console used above in Spyder is a little different from a normal Python shell, but for our purposes we can treat them as more or less identical.

If you play around with both tools, you may notice that if you input a line of code that’s the first line of some larger block of code, like a for loop or conditional statement, you will be allowed to enter additional lines of code after the first. Press enter twice after your last line of code, and the whole block will run.

Script Mode

If instead you are working with more than a few lines of code, or you’re ready to write an actual program, script mode is what you need. Instead of having to run one line or block of code at a time, you can type up all your code in one text file, or script, and run all the code at once. In the standard Python shell you can go to “File” -> “New File” (or just hit Ctrl + N) to pull up a blank script in which to put your code. Then save the script with a “.py” extension. You can save it anywhere you want for now, though you may want to make a folder somewhere to store your code as you test Python out. To run the script, either select “Run” -> “Run Module” or press F5. You should see something like the following:

script mode example

If instead you are using Spyder, here’s what running the same code in script mode will look like. The process is similar, except you’ll click the green “Run file” button in the tool bar (or press F5 again) to run the script.

script mode example spyder

As you’ll see, if you’re working with more than a handful of lines of code or you’re building an actual program, script mode is often the way to go. Here’s a simple example with more than a single line or block of code that you’d have to enter in separate commands in interactive mode.

script mode example spyder 2

Use print() to display values in script mode

Something to note is that some code will result in something being displayed when executed in interactive mode, but that same code will be ignored in script mode. For example, try running 1 + 1 in both modes. Interactive mode will display the result of the calculation, while script mode won’t result in anything being printed to the console. This is where the print() function comes into play, as seen earlier in this post.

Executing Lots of Code in Interactive Mode

You might find, however, that there are times when you want to run lots of code but don’t feel like saving a script. In this case, there are a couple things you can do to make interactive mode run more code.

The first way is to simply copy and paste all the code from wherever into the shell or IPython prompt. Often this will run all the code just fine.

The second way, and what I often find most convenient, is to copy and paste the code in question to a blank script in Spyder. Then you highlight however much of the code you want to run and hit Ctrl + Enter. This will run all the highlighted code interactively without you ever having to save a script. This makes it really easy to quickly test out different parts of a script and is a nice mixture of interactive and script mode.

Related Posts

Local Interpretable Model-agnostic Explanations – LIME in Python When working with classification and/or regression techniques, its always good to have the ability to ‘explain’ what your model is doing. ...
Python – TechEuler Python – TechEulerUnOrdered Linked list – Prepend, Append, Insert At, Reverse, Remove, SearchUse of __slots__ in Python ClassUsage of Unde...
Introduction to Python Ensembles Stacking models in Python efficiently Ensembles have rapidly become one of the hottest and most popular methods in applied machine learning. Virtually...
Postgres Internals: Building a Description Tool In previous blog posts, we have described the Postgres database and ways to interact with it using Python. Those posts provided the basics, but if you...