How to Run a Python Script

Reading Time: 4 Minutes
Publication date:

A beginner Python developer should not only be familiar with syntax and basic operator usage but also be capable of executing code in this language. This is because it is only after writing the code that you can understand whether the script is working or if there are errors. Here, we will discuss in detail how to execute scripts from the operating system terminal, integrated development environment, or just the OS interface. This will allow you to choose a suitable option and increase the efficiency of your work.

Python Interpreter

The programming language in question is currently one of the most progressive languages. It allows solving tasks quickly and efficiently in various fields. However, the word “Python” also refers to an interpreter, i.e., a program on the computer that allows executing the written script. It presents an additional software layer between the PC hardware and the code.

Programs written in:

  • C programming language;
  • Java programming language;
  • Python;
  • .NET environment.

The specific choice does not matter much to the end user. Regardless of the type of program, the written code will be executed as specified by the language rules.

Execution can be done in two ways: as a ready-to-use software sequence (script or module) or as individual pieces of code entered directly into the program window.

Running Interactive Code

You can test individual commands using the interpreter in an interactive session. To do this, you need to open your operating system’s command prompt and enter the command that starts the interpreter.

For Linux operating systems, it looks like this:

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Now you can enter commands that will be executed immediately. The downside of this approach is that all sequences entered are not saved once the current session is closed.

Interactive code execution is necessary for quickly testing a part of the written code. Additionally, it can be used in the learning process to verify the functions of specific operators “on the fly”. Interpreting commands in this way allows you to try out specific language features without the need to write a separate script.

You can exit interactive mode by using the quit() command or simply closing the terminal window on Windows.

Starting the Interpreter Before Opening a Terminal or Command Prompt:

  • On Windows, press the “Flag” + “R” key combination, then enter the cmd command and click the “OK” button in the dialog box.
  • On Linux or similar operating systems, the command prompt is available through a separate program. You can use xterm or Konsole.
  • On macOS, to access the terminal, select the “Applications” menu, then go to “Utilities” and click on “Terminal”.

How the Python Interpreter Works for Python Scripts

The execution of a written script or module is done in packet mode. It follows a complex scheme, which includes the following stages:

  1. Sequential processing of all operators written in the script.
  2. Compilation of the initial stage into an intermediate format. The interpreter creates byte-code, a lower-level programming language independent of the platform and operating system. Byte-code is necessary to optimize the script execution process.
  3. Execution of the obtained code. At this stage, the Python Virtual Machine (PVM) is activated, which cyclically converts each operator from the script and starts it for execution, similar to entering each command sequentially in the interactive interpreter.

Running the Script in the Command Prompt

In interactive mode, as discussed above, you can write and run as many lines of code as you want, but they are not saved once the terminal window is closed. Therefore, real Python programs are written as scripts, which are ordinary text files. To avoid confusion while storing them, they are given the .py or .piw extension.

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

#!/usr/bin/env python3
print('Hello World!')

Save the file with any name and the .py extension in your working directory.

To execute the script, the programming language interpreter should be used, utilizing the file’s name as an additional parameter.

$ python3 hello.py
Hello World!

In the example above, the file is named hello.py. After typing the command, you will need to press Enter, and the result of the script’s work will appear on the screen, i.e., “Hello World!”

If the program file is not saved in the interpreter’s catalog, you will need to specify its path.

Re-evaluating Output Devices

When executing program code in Python, it is often necessary to store the results displayed by the program. These results can be analyzed later for errors or other purposes. In this case, the script should be started with the following command:

$ python3 hello.py > output.txt

A file named output.txt will store everything that would be displayed on the screen during the script’s execution. This is a standard syntax provided by the operating system.

If no file with that name exists, the operating system will create it automatically. If the file does exist, any existing data will be overwritten without saving. To append data to the end of the text file instead of overwriting it, use two >> symbols instead of one >.

Starting from the Command Prompt without the Interpreter

In the latest versions of the Windows operating system, there is an option to run a Python script from the command prompt without typing the program interpreter’s name. This means you only need to type the file name with the extension.

C:devspace> hello.py
Hello World!

This is because when you click on a file or run it from the command prompt, the operating system automatically searches for the relevant application and runs it. Similarly, you can open Word files by clicking on them.

In Unix, you can also run scripts this way. However, for this, you need to add the text #!/usr/bin/env python at the beginning of the text file with the command. This specifies the program used to run it. The interpreter of the programming language treats this line as a comment and skips it.

Running Scripts in Interactive Mode

In interactive mode (as described in the previous section), the user can load a file with a pre-written sequence of commands and execute it. This method can be used when the module contains functions, methods, or other operator calls that generate text on the screen. Otherwise, there will be no visible result of the program’s work.

To execute the script from an interactive session, the following command can be used:

import hello
Hello World!

Note that this command works only once in an interactive session. Therefore, if you make changes to the script file and run it again with this command, nothing will happen.

Conclusion

Now you know that Python commands and scripts can be run in various ways and modes. This allows you to choose the appropriate option to solve a specific task, increase your work speed, and make it productive and flexible.

5 / 5. 1

Share:

Leave a comment