How to run Python Program?
Python programmers must know every possible way to run the Python scripts or code. This is the only way to verify whether code is working as we want. Python interpreter is responsible for executing the Python scripts. Python interpreter is a piece of software which works between the Python program and computer hardware. Here we are describing the series of ways to run Python scripts.
The operating system command-line or Terminal
We can run the Python code using a command line because in Python shell once we close the session, we will lose the complete code that we have written. So it is good to write a Python code using the plain text files. The text file must save as .py extension. Let's see the following example.
We write the Python print statement and save it as sample.py in working directory. Now, we will run this file using the command-line.
Open the command line to run a Python script. We need to type the python, followed by the file name to execute the file. Now, hit the enter key, and if there is no the error in file, we will see the output as follows.
The Python interactive mode
To run the Python code, we can use the Python interactive session. We need to start Python interactive session, just open a command-line or terminal in start menu, then type in python, and press enter key.
Here is the example of how to run Python code using interactive shell.
It allows us to check every piece of code, and this facility makes it an awesome development tool. But once we close the session it will lose all code that we have written.
Below are the few options to exit the interactive mode.
The IDE or Text Editor
The IDE stands for Integrated Development Environment. There are various IDEs but Pycharm is Python's most popular and useful text editor among them. It is recommended for developing large and more complex applications. Here are using the Pycharm to run python script.
Create a new project and then create a new Python file using the .py extension.
Now, click on the green button and it will show the output as follows.
The file manager system
The Python file can run by double-clicking on its icon in a file manager. This process may not be widely used in the development stage. The file should be associated with the .py or .pyw with the program python.exe, respectively.