×

How to run Python code from the command prompt

The Windows operating system's command-line interpreter is CMD or Command Prompt. The "MS-DOS Prompt" is comparable to Command.com, used in DOS and Windows 9x computers. It is similar to Unix Shells used on systems that resemble Unix. The user can carry out activities using instructions using the command prompt, a native Windows application.

Python in cmd

You can run python scripts from the command prompt using the python commands.

Depending on which version you installed on your system, you can use Python in the Command Line by typing Python, and different versions specify the python version present on your computers, like Python3, Python3.7, Python3.8, or Python3.9. You'd have to be more precise if you installed multiple versions. The well-known REPL prompt will appear after that.

Typing python may send you to the Microsoft Store to download the necessary components if you have never used Python before and have not installed the application. And opens the python application if python software is present in your system.

How to open a command prompt?

To open a command prompt in your system, you can follow the below-mentioned steps:

  • Click on the start button
  • Go to the search bar and type command prompt, or you can also type cmd.
  • Click enter

Now a prompt will be opened by doing so.

You can also open cmd using another method. The method is:

  • Click Windows + R
  • A run box will be opened, and in the search bar, type command prompt or cmd and click enter.

Using the two methods mentioned above, you can open the command prompt.

Using python scripts in Command Prompt

 You can run python scripts from the command prompt using the python commands.

Any plain text editor can be used to produce Python code files. Sublime Text is a robust and user-friendly editor you can use if you're new to Python, although you can utilize any editor you choose.

You must open a command line and input the word "Python" (or "Python3" if you have both versions) followed by the directory of your script to launch Python code with the python command. And press enters to run the python code specified above. The output will be displayed on the command prompt screen itself.

For instance, let us consider a simple python code for understanding how to run python code in a command prompt.

Example

print ("hello, welcome to coding")
# List in Python
l = [1, 2, 3]
# Printing the list elements
for i in l:	# For loop for iterating over a list
	print (i)

Now save the python file as demo.py.

Open the command prompt using the steps mentioned above. And type the following command.

$ python3 demo.py

Typing the above command in the command prompt, your python code will be executed, and you will be able to see the output on the command prompt screen.

Python software must be present in your system; to use the above command, you will get an error if python software is not currently in your system. You can install Python from the browser, a free application, and download it from the Microsoft store.

You can also run python scripts using the following options:

Running Modules With the -m Option

You can use any of the Python command-line parameters following your requirements. For instance, you can use the command python -m module-name> to start a Python module.

For example, you can run the above python script demo.py using the -m option, which runs the demo.py file as a module and executes the code accordingly.

Following is the command for doing so; you need not mention the py extension here.

$ python3 -m demo

Typing the above command in the command prompt, your python code will be executed, and you will be able to see the output on the command prompt screen.

Using Script file name

Python scripts can be executed on more current versions of Windows by simply typing their file name at the command prompt:

You can open the command prompt at the python file location or navigate the command prompt to the file location and type the file name with the python extension.

Below is the command for doing so,

C:\location> demo.py

Windows uses the system memory and file associations to decide which software to launch a specific file, making this feasible.

This line specifies which application must be used to run the file for the operating system; for Python, it is just a simple comment.


Related Topics

Python Comment Block

In this tutorial, we will see what comment blocks mean in Python. Further, we will see the commenting methods supported in Python. We will understand the topics deeply with the...

6 minutes read.

Python program to find the area of the triangle

Python program to find the area of the triangle This article will discuss how to find the area of a triangle in Python with all three given sides. The area of...

2 minutes read.

What does the if __name__ == "__main__" do in Python

In this article, you will learn about the If__name__==__main__ is a statement in python to define modules and the names of the modules. This statement plays a vital role in...

3 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Python object()

Python object() class The object class in Python is a base for all classes. It returns a new featureless object. Syntax class object Parameter NA Return It returns a new featureless object. Example 1 # Python program explaining # the object()...

1 minute read.

Python MySQL Delete Operation

Python MySQL Delete Operation: Like the update operation where we were updating required field from a SQL table, we can also delete an entry from the table which we have...

4 minutes read.

Python List append() Method

Python List append() Method The list.append() method in Python adds or appends an item to the end of the list. Syntax list.append(x) Parameter x: It is a required parameter that represents an element of any type (string, number,...

1 minute read.

IPython Display

A command shell, often known as IPython is a software application that makes an operating system's features accessible to users or other applications. Based on the purpose and specific functioning...

6 minutes read.

Google Python Class

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Super function

The super function is used to access and call the methods or functions involved in the parent class during Inheritance. What is Inheritance? The process where the parent class inherits some of...

3 minutes read.

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Python Abstract Class

An Introduction to Abstract Classes An Abstract Class is one of the most significant concepts of Object-Oriented Programming (OOP). An Abstract class can be deliberated as a blueprint or design for...

6 minutes read.

How to Open a File in Python with a Path?

File in Python File handling is an important operation; it allows the programmer to access the data in the files, such as text, binary values and excel sheets. The CSV files...

7 minutes read.

Best resources to learn Numpy and Pandas in python

In this tutorial, we will discuss the different resources where we can learn about NumPy and Pandas libraries of Python in a more efficient way. NumPy NumPy, a Python library used for...

4 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

What Does the Percent Sign (%) Mean in Python?

In python, the percent sign is called modulo operator " %, " which returns the rest of partitioning the left-hand operand by the right-hand operand. Example: value1 = 8 value2 = 2 remainder =...

4 minutes read.

Weight Conversion GUI using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

Python program to sort the array element into ascending order

Python program to sort the array element into ascending order In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a...

2 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.