How to run a Python script

A beginner Python developer, in addition to the syntax and basic rules for using this language, should be able to run code for execution. After all, this is the only way to understand whether the written script is working. Let’s take a closer look at how you can run scripts in the terminal of the operating system of an integrated development environment or simply from the OS interface. This will allow you to select the appropriate option and increase your efficiency.

Python interpreter

The programming language in question is one of the most progressive at the moment. It allows you to quickly and efficiently solve problems in a wide variety of areas. However, the term Python is also understood as an interpreter, that is, a program on a computer that allows scripts to be run for execution. It is an additional software layer between the PC hardware code and the code.

There are several interpreters:

written in the C programming language;

written in the Java programming language;

written in Python;

programs implemented in the .NET environment.

The choice of a specific option for the end user does not matter. Regardless of the type of program, the written code will be exactly as prescribed by that language.

Launching is possible in two ways: as a ready-to-use program sequence (script or module) or as separate pieces of code, entered directly into the program window.

Run code interactively

You can use the interpreter in interactive session mode to test individual commands. To do this, you need to run the system interpreter and toolkit.

Now you can enter the commands that will be immediately after that. The disadvantage of this approach is that the entire entered sequence is not saved after the current session.

Interactive code execution is necessary in order to test a piece of written code. In addition, it can be used in the training process to test the actions of certain operators «on the fly». This way of interpreting commands allows you to try out the language functions you need without having to write separate scripts for this.

You can quit interactive mode with the quit () command or simply by closing the terminal in Windows.

To open a terminal or system, before starting the interpreter itself, you must:

In Windows, press the Check Box + R key combination, and then click OK in the dialog box.

On Linux or other systems, access to the system is provided through additional programs. You can use xterm or Konsole.

In macOS, to access the terminal, select the Applications menu, then go to the Utilities section and click on the Terminal element.

How the Python interpreter for scripts works

The written scripts or modules are launched for execution in batch mode. And it is performed according to a complex scheme, which consists of the following stages:

Sequential processing of all statements that are written in the script.

Compilation of the original move into an intermediate format. The interpreter generates bytecode, which is a low-level programming language that is platform and operating system independent. Bytecode is needed to optimize the script execution process.

Execution of the received code. At this stage, the Python virtual machine (PVM) comes into play, which loops through each statement from the script and launches it for execution. How do you enter each sequence into an interactive interpreter.

Running scripts on line by line

In the interactive mode discussed above, you can write and execute any number of lines of code. But after closing the terminal window, they are not saved. Therefore, real ones in Python are written in the form of scripts and replace ordinary text files. To avoid confusion during storage, they are assigned the .py or .piw extensions.

You can create a text file using any editor, including Notepad. However, it is better to use more advanced solutions like Sublime Text. For example, let’s take the simplest script with which familiarity with any programming language.

Running scripts from interactive mode

Being in the interactive mode (described in the first section), the user can load a file with a previously written sequence of commands and run it for execution. This method can be used when the module contains calls to functions, methods, or other operators that generate text on the screen. Otherwise, there will be no visible results of the program.

Conclusion

Now you know that Python commands and scripts can be run in different ways and in different modes. This will allow you to choose the right option for solving a specific problem, increase the speed of your work, make it productive and flexible.