How to Compile and Run Python Program using Python Shell and CMD

compile and run python programs in cmd and shell prompt

A Python program is converted into a byte code after compilation. This produced byte code is run in the Python Virtual Machine in interpreting mode. Let us compile and run Python programs using a Python Shell and CMD (Windows Command Prompt) in this Last Minute Python Tutorial.

Remember that you will get the output of a program using a single command or using a single RUN operation. We have used the Windows 11 operating system in this tutorial.

1. How to Compile and Run Python Program using CMD (Command Prompt)

This CMD or Command Prompt is also called DOS Prompt or terminal. Many programming languages support compiling the programs in command prompt. CMD exists in all windows operating systems including Windows 11, Windows 10, Windows 8.1, Windows 7 and Windows XP.

Let us create a sample Python program with the name "hellopythoncmd.py".

hellopythoncmd.py

print('Hello Python CMD. How are you?')

The program contains only one Print statement.

Let us compile and run the above Python program in CMD as shown below.

c:\python> python hellopythoncmd.py
Hello Python CMD. How are you?
c:\python>

Observe that a single command "python" compiled the program "hellopython.py" and displayed the output on the screen. In other programming languages like Java, getting the output requires you to compile the program with one command and run it with another command.

2. How to Compile and Run Python Program in Python Shell

Python shell looks similar to a DOS prompt but with different commands-set which are specific to Python language. Python Shell comes with an IDLE (Integrated Development Learning Environment) with a Menu bar.

Let us create a sample Python program "hellopythonshell.py" in a folder say "C:\Python".

hellopythonshell.py

print('Hello Python Shell. How are you?')

Let us follow the below steps to Compile and Run the above Python program in Python Shell IDLE or IDE.

Step 1: Open Python Shell IDLE. It looks more or like a Notepad editor with a menu bar and Shell prompt (>>>).

Step 2: If the program is not available as a file, you can casually enter the program and press enter to see the output like below. This interactive mode is not suitable for big programs with so many variables and classes.

Running python code in Python Shell interactive mode

Step 3: To load an existing python program present on the hard disk into the Python IDLE, click File and choose Open. Choose the file "hellopythonshell.py".

File Open option in Python Shell IDLE

Step 4: Opened python program looks like this with a cursor to edit. You can type the program of any number of lines in the editor.

Python file editor in Python Shell IDLE

Step 5: Click the option "Run". You will the output in a new window like below.

Python program output in Python Shell IDLE

That's it. This is how you compile and run a python program using the two methods above namely CMD mode and Python Shell mode.

Share this Last Minute Python tutorial with your friends and colleagues to encourage authors.