Home » Python » How to run Python Script

How to Run Python Scripts

After installing and setting up the environment variable, the next thing is to learn about how to run Python scripts. There are different ways of executing a python script and each has its own advantages. In this tutorial, we will guide you through different ways in which you can run a python script on your computer. First, let's learn about Python Interpreter.

What is Python Interpreter?

Python is an interpreted language just like PHP. As we have told you earlier, Python needs to be interpreted, which means, it will get executed line by line.

A computer doesn't understand Python language, it just runs on machine language and system calls. Hence, a python interpreter transforms the python code line by line into Machine Language, so that the computer understands it and performs the given instruction.

In simple terms, Python interpreter is a software, that is used for communication between the Python script and the computer to seamlessly run the program.

Types of Python Interpreters

There are different Python Interpreter for different implementation of Python:

  • CPython: It is the most widely used interpreter. It is most compatible with Python packages and C Modules. It is written in C Language and Python.
  • Jython: It is written in Java and Python. It converts Python code into Java bytecode and allows importing Java Modules in Python.
  • PyPy: It is written in RPython(Subset of Python). It is faster than CPython.
  • IronPython: It is an implementation of Python for the .NET framework. It comes integrated with Visual Studio and can use .NET Objects.

Python Script Execution types

Python can be executed by 3 types:

  • Interactive Mode
  • Script Mode
  • Using IDE
Let's go through each one by one-

1. How to Run Python Code Interactively

An interactive mode is nothing but executing python commands directly by using the Command prompt. It is a fast way to see the result of any Python code. It is useful when the amount is code is small, around 1-2 lines only.

Just open the command Prompt and type python and hit enter, Now you have entered in the Python terminal and can execute small Python commands.

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Let's print something, we will use python's print() function to print "Hello Python!" on the terminal. This print method is used to print something on the screen. Look at the image below, after we hit enter, "Hello Python!" gets printed on the terminal.

Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 22:20:52) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello Python!")
Hello Python!

To exit this Python Terminal, just enter exit() and press enter.


2. Script Mode

The script mode means creating a script of Python and then executing it. The terminal mode is useful for single statement execution but if we want to execute a large amount of code then we would have to save the code first and then execute it.

The Scripts are created with .py extension. These scripts contain multiple lines of Python code. You can create these scripts in any editor like notepad but always save it with .py extension.

Let's save the command print("Hello Python!") in a file and name it Hello.py. Now to execute, we simply have to open the folder in which the file resides and then has to type python Hello.py and press enter.

print ("Hello Python!");

C:\Users\CodeRepublics\python helloworld.py

It will print Hello Python! as the output of the script. You can enter multiple statements and execute them all at once using this approach.

Using IDE

IDE is Integrated Development Environment software that provides a GUI based execution environment for Python Scripts. In an IDE, you can use both Interactive and Script mode with a lot of other functionalities.

Some popular Python IDE's are IDLE, Visual Studio Code, PyCharm, etc. Install these IDE's and start creating Python programs.

Color Picker

List of color names supported by all browser »

Character Code

List of character codes to display special character »

Language Code

List of all language supported by all browser »

Country Code

List of Country code supported by all browser »