How to execute python scripts from command line???

    Python is a popular language nowadays. The python scripts are the python codes created by the developer. These can be created in many ways. But it needs an interpreter in the system, where you run the program.

Command line is an interface between the user and the system. It gets the commands as a text in the command prompt, executes and gives you the output in the screen. 

How to enter the command line:

              To open the command prompt, click the start button, type “cmd” in search bar.


Next, select the command prompt icon. It opens the command prompt.


How to execute python scripts from command line:

There are many methods to execute the python scripts. They are listed below…

  • 1.       Run the python script directly in command prompt.
  • 2.       Run the python file which contains python code.

1.Run the python script directly in command Prompt:

   The steps to follow is given below.    

  •    First, open the command prompt and type the keyword “py”.
  •    Type the python code line by line in the command prompt.

    For example,  Creating a welcome message in python.

    The code is given below….

     #Python code to print the welcome message

      print(“Welcome to the world of python”)

  • just type the code and press “enter” button. It gives the below output.

2.Run the python file which contains python code:

  For this method, follow the below steps.

  • ·       open notepad and save the file as “welcome.py”.
  • ·       Add the code to the file.

#Python code to display welcome message

print("This is an example of python file")

print("Welcome to the world of python")

  • ·       save the file and run in the command prompt by typing this.

·                               py welcome.py

·       It gives you the output.

These are the ways to run the python scripts in run time.

No comments:

Post a Comment