As you know if you are plan to enter the world of python programming the first and most important thing you should understand is how to run Python Scripts or code.
So In this article, we are going to discuss this thing. But before then that let’s understand python. Nowadays python is being one of the leading programming languages. It has a very easy syntax which makes it easier for those who are their initial stage of learning. It is also very useful to handle large databases and data science projects.
lets’s understand the difference between code and scripts
- Code: Code is a language that is converted from human language into, machine language that computers can understand.
- Scripts: Script is a simple program stored in plain file text that contains python code.
Step To Run Python Scripts
Python Interpreter :
The interpreter is a layer of software that works as a bridge between program and hardware to run the code. A python interpreter is an application that is responsible for a handle or runs the python scripts.
The python interpreter work on REPL ( Read Eval Print Loop ) environment.
- Read the command
- Evaluates the command
- Print the result
- Loopback and process the repeatedly.
Starting the Python Interpreter
- The simplest and easy way to start the interpreter is to first open the terminal from the command line.
- To open the command-line interpreter
- In window to go to the command prompt go to start menu -> Run(Press Windows + R) and type cmd
- For Linux It is accessible by many applications like xterm Gnome Terminal or Konsole.
- For Mac OS X it is accessible through Applications → Utilities → Terminal.
Running Python Code Interactively
To run a python by an interactive session is a broadly used way. It is an excellent development tool with the language that allows you to test every piece of code at a time on the go.
To start the interactive session type python in the command line or terminal and press ENTER key from the keyboard.
The image has shown below how to do this on windows
The example has shown below how to execute code in interactive session.
The only disadvantage of that is when you close the interactive session the code will no longer exist.
The PEM (Python Execution Model) is given the multi step process to run python scripts.
- First of all the statement or the expression of your scripts are processed in a sequential manner by the interpreter.
- After than code is compiled into a set of instructions called byte code.
- Bytecode is an intermediate machine-independent code that optimizes the process of code execution.
- That’s why the interpreter ignores the compilation step when executing the code for the next time.
- At last, the interpreter transfer the code for execution.
- The Python Virtual Machine(PVM) is a part of the python environment that is installed in your system.
- PVM loads the code in python runtime and reads each and every operation and it is the component that actually runs your scripts.
Run Python Scripts Using Command Line
- You can also run your code or scripts by using a plain text editor.
- As we said above the code written in interactive session has lost when the session is closed.
- If you are at the learning stage of python you can use an editor like sublime or Notepad++ which is very easy to use
- Now you need to open any editor which you like and write the following code: print(‘Hello World!!!’)
- After writing the code save the file with the extension .py.
- Only you need to give a py extension.
Run Using Python Command
- The most basic and easy way to run python scripts is by using python command.
- Open the command line end type the python followed with the file name like this
- python file_name.py
So this is the various way step by step how you can run python scripts.