×

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 String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.

Python Namespace

In python, the namespace is a very important concept that should be understood before using any function or variables. When we write code, we often use variables, libraries, functions, modules, etc....

4 minutes read.

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it. Now, let us...

3 minutes read.

Python range() function

Python range() function The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. Syntax range(stop)        or range(start, stop[, step]) Parameter start: It is...

1 minute read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Traverse Dictionary in Python

Traversing a dictionary is visiting each and every step in the dictionary, which is also called iterating the dictionary In Python, dictionaries are a useful and commonly used data structure in...

3 minutes read.

Python String encode() method

Python String encode() method The string.encode() method in Python returns an encoded version of the string. The default encoding is the current default string encoding Syntax String.encode([encoding[,errors]]) Parameter Encoding: This parameter represents a String specifying...

2 minutes read.

Python And Operator

Python's and operator takes two operands that can be either object, Boolean expressions, or both. And operator creates more complex expressions using those operands. Conditions are the common name for...

8 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

How to Install Numpy in PyCharm

What is PyCharm? JetBrains created the hybrid platform known as PyCharm as a Python IDE. The Python IDE PyCharm is used by some major companies, including Twitter, Facebook, Amazon, and many...

4 minutes read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

How to Call a Function in Python

How To Call a Function in Python Functions are the well-defined and structured piece of code that is used to implement specific functionality. Calling a function in python is the best...

4 minutes read.

GUI Calculator in Python

Introduction In Python, we can develop a GUI(Graphical User Interface) with multiple options. It offers us some great and commonly used methods for the development of the Graphical User Interface. Tkinter...

4 minutes read.

Front end in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

4 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Python bool()

Python bool() class The bool() class in Python returns the boolean value for the specified object. Syntax class bool([x]) Parameter object: This parameter represents the object, like String, List, Number etc. Return It will always return True, except...

1 minute read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Python MySQL

In this article, we are going to learn the following: How to connect Python to MySQL.How to create a new Database.Procedure for connecting the newly created database.Procedure for connecting the already...

7 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.