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.