How to Run Python Program?

by | Nov 12, 2019 | Python Programming

12 Min Read. |

Python is by far one of the most popular languages for running computer development applications and is slowly becoming an industry standard, challenging the likes of setups like C++ and RStudio. It’s used for practically everything from scientific computing to software development. It’s an important skill for people to know how to run python programs, not just in the traditional way but in all the other methods that make the process of execution faster and automated.

Did you know that the language’s creator-Van Rossum actually came up with the name for it by referencing the famous comedy sketch group “Monty Python’s Flying Circus”.

There are more than 147,000 packages used for everything from data analysis to game development. Collectively they are referred to as the ‘cheese shop’, again referencing the comedy troupe. Still doubtful as to how Python is important. First of all, you should know the basics of Python Programming. This will give you a better understanding of how to run Python Programs.

Let’s just say that the language has surpassed Google search hits for big names like Donald Trump and Kim Kardashian. And it’s popularity is only matched with its simplicity.

In this section, we’ll look at some questions that coders might have such as how to run python program in cmd or how to run python programs in windows.

The essential difference in understanding all these methods to know how to run python programs is the ability to run the program from any kind of a system without any complications. As one will see in the following sections, Python programs can be coded to be executed in a number of ways that can save people considerable amounts of time.

A Standard File-Using The Command Line

All python programs-whether in notebook formats, shell, scripts or notepad texts will have the .py extension. Python files can be run using the command line terminal, through the python IDE or by using a standalone interactive mode under the Anaconda library. Let’s start with the command line terminal first and see how to run python program in windows.

A Standard File-Using The Command Line

A Standard File-Using The Command Line

To know how to run python programs & execute and run a file through it, users will first have to specify the exact file location for the executable file. The physical location of the file on the disk has to be specified using text separated by slashes.

This is similar to how files are referenced and called in Linux systems used the command line or telnet. One of the most common problems that students and new users face in knowing how to run python programs is a barrage of errors that can be fixed and prevented beforehand.

Download Detailed Curriculum and Get Complimentary access to Orientation Session

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
  • This field is for validation purposes and should be left unchanged.

Always remember to keep all the files that are being referenced in a common file path or preferably in the same folder so that the system does not read it improperly. Once the python file has been written and saved, have a code debugger check for any instances of errors or bugs. If you’re using text editors like notepad or notepad++, debugging would become highly difficult.

While some may seem comfortable using simpler systems for code writing and editing, a majority of the time that goes into running python programs from standard command lines is often spent in debugging and looking for errors. Using a code editor along with the text file is suggested.

Using a Code Editor along with the Text File

Using a Code Editor along with the Text File

Open up the command terminal and type -cd along with the location of the file. This will open the file and make it ready for edits. To run it, have a version of python installed on the system and then type -cd python [file location].

This will cause Python to access the file location and use any existing packages to run it. The system may prompt you to let python access the file at which point you should allow it.

How to Run Python Program Scripts And Modules

Another thing that you should keep in mind while knowing how to run python programs is the debate between using scripts or a module continues to rage even today. It’s preferable to just use a system that seems comfortable and easy to work with.

Scripts will need to be processed by some kind of a processor which would then go to an interpreter where the commands would be sequentially executed. Module files like those that are written in plain text are imported into the system and then directly executed.

Let’s talk about the interpreter. It’s a nifty piece of software that you would need to Python codes and scripts. As the name implies, an interpreter reads through the code and acts as a bridge between the program and the computer hardware to run the code.

Interpreters aren’t always software or platforms. They can be a program that is written in some other language but then converted into Python(like CPython), Java programs written in Jython, IronPython files that are written in .NET. Interpreters can run the code in two different ways.

The first being through a script or module as discussed previously or by executing the code on real-time through an interactive session(similar to what users execute on Anaconda or Jupyter Notebooks).

In order to run an interactive session, first, make sure that the interpreter is properly downloaded and can execute files with no errors or additional packages required. Before getting to executing big files, type in the following information in the command line to know more about the software.

Information in the command line to know more about the software

Information in the command line to know more about the software

$ python3

Python 3.6.7 (default, Oct 22 2018, 11:32:17)

[GCC 8.2.0] on linux

Once you’re done executing the commands, type in exit() or quit(). What about scripts? Type in the code in a text editor with the same syntax and rules. Type in the following command:-

#!/user/bin/env python3

print('Hello World!')

Save the file in your working directory any file name as long as the extension is .py. If you need to redirect the results of the output somewhere else or for future use, type in:-

$ python3 > test.txt

Running Modules

Another way to know how to run python program modules is by using the command -m <module-name> rather the options listed before. The -m option will search for the system path and run the content contained in it. On the recent versions of Windows, one can run Python scripts using a simple insertion of the file name containing the code at the command prompt.


Module registries for running the code can be done using two methods on Windows and Linux systems:-

(i) #!/usr/bin/python, where the user would enter the absolute path.

(ii) #!/usr/bin/env python, where the user would have to use the operating system env command.

Hello World!

Files can also be run on modules and script command interfaces like Vim. Insert the following code after saving it to a file on a local path in Vim:-

This will produce an away that can be used to see the element-wise operators for the entire system.

Running Python Interactively

When users import modules they are essentially transferring the contents of a module stored on a physical space to for later access and use.

If the code contains classes, functions and plain commands, the execution is instantaneous. But when you load a file that calls functions, statements or packages with visible results, seeing execution is much easier.

Download Detailed Curriculum and Get Complimentary access to Orientation Session

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
  • This field is for validation purposes and should be left unchanged.

As an example, the question as to how to run python programs in cmd can be first answered by looking at the command>>> import hello will generate the Hello World! Response since it is stored as an import call on the python environment. In order to know the exact file location for the python file that is being referenced, it is important to use the Python Module Search Path (PMSP), where Python looks for the modules and packages you import.

To know what is contained in the  PMSP, you can run the following code:

>>> import sys

>>> for path in sys.path:

...     print(path)







Use the import command to test out some of the commands from these packages.

>>> import importlib

>>> importlib.import_module('hello')

Hello World!

<module 'hello' from '/home/username/'>

>>> import hello  # First import

Hello World!

>>> import hello  # Second import, which does nothing

>>> import importlib

>>> importlib.reload(hello)

Hello World!

<module 'hello' from '/home/username/'>

Remember that the argument of reload() has to be the name of a module object, not a string:

>>> importlib.reload('hello')

Traceback (most recent call last):


TypeError: reload() argument must be a module

But be cautious, if the command has a string, then reload() will raise a TypeError exception. If the system is using Python 2.x, then you’ll have imp, which is a module that contains commands called reload(). imp.reload() works similarly to importlib.reload().

>>> import hello  # First import

Hello World!

>>> import hello  # Second import, which does nothing

>>> import imp

>>> imp.reload(hello)

Hello World!

<module 'hello' from '/home/username/'>

Using Runpy and Numpy

Open up The Standard Library and you will see a module called runpy. In this module, you can find run_module() that can run modules without importing them. Here’s how to use them for the purpose of dry running python commands:-

>>> runpy.run_module(mod_name='hello')

Hello World!

{'__name__': 'hello',


'_': None}}

Runpy also provides run_path(), which can execute a file provided its location in the filesystem is provided:-

>>> import runpy

>>> runpy.run_path(file_path='')

Hello World!

{'__name__': '<run_path>',


'_': None}}

Like run_module(), run_path() returns the globals dictionary of the executed module. Python programs can be run using exec() as well, which is a built-in function that supports the dynamic execution of Python code. exec() can be run on Anaconda as well.

>>> exec(open('').read())

'Hello World!'

Certain commands can be run using the os module as well in python terminals that will show retuned outputs for code but may not be appropriate for more complex code for machine learning.

import os

stream = os.popen('echo Returned output')

output =


'Returned output\n'

Using Spyder and Notebooks

Jupyter Notebooks and Spyder are excellent additions to have if you ever plan on performing more specific coding tasks for high-level operations such as creating games, setting up model applications, testing animations and much more.

Check out the following video to get a breadth of understanding as to how python files can be run:-

The notebook format of inserting and executing code chunks is similar to an IDE where the user has to specify what the system will be running and what it will not. If you open Jupyter Notebooks, click on New and select a Python file to work on. You will be met with a blank screen and a user input line where the code can start.

Begin by adding the normal import functions to test out the code and click on the run button at the top of the page. As the system will import the specified packages, make simple operations for mathematical expressions to test more code.

An important difference between code in Python Notebooks is that the system does not remember the code configurations once the file is downloaded and the terminal is stopped. This means that the user has to repeat the same setup steps when opening the file for the first time.

Notebook files end with the ipynb extensions and can be used simultaneously on a compiler. Be warned, however, that setup steps between shell terminal files and python notebooks are different. Conda install and pip install are the common variants used for installing everything required to keep program functioning.

In order to check if a particular item is installed, simply install the system package:-

Import sys

Then insert a command for sys by calling it and writing the component that has to be checked. If it is not installed, the output will read false. Spyder is a continuous coded IDE that makes the process of running python programs simpler by having an input and output file window as well as a more efficient debugger that specifies what errors have been made in the code along with the lines.

Hit the tab button at all times whenever coding to know the full command or to autofill a line. This is an important function to save time and learn code on the go. Running components like pygame are more suited for game files as the interface is compatible with the output structures.

How to Run Python Program Scripts From a File Manager?

Running a script by double-clicking on its icon in a file manager is another possible way to execute Python programs. It’s not widely used in the development stage, but it may be used when the code is released for production. Some conditions must be met before doing so though.

Windows, for example, associates the extensions .py and .pyw with programs python.exe and pythonw.exe respectively. By downloading software like Xcode and Pycharm, these files can be executed by just double-clicking on them.

On platforms like Anaconda and Jupyter labs, the code is essentially separated into a series of chunks and segments that are executed depending on the user. Some users may find it difficult to import packages using the import function in such platforms. If you’re using Anaconda, the pip command and not the import command will install the required package on the system. The particular issue with the import function on such platforms is that some might have to be installed every time a new file is generated.

Because the execution of scripts through double-click has several limitations and depends on many factors (such as the operating system, the file manager, execution permissions, file associations), it is recommended that you see it as a viable option for scripts already debugged and ready to go into production.

Download Detailed Curriculum and Get Complimentary access to Orientation Session

Date: 23rd Jan, 2021 (Saturday)
Time: 10:30 AM - 11:30 AM (IST/GMT +5:30)
  • This field is for validation purposes and should be left unchanged.


This tutorial on how to run Python Programs should serve as a starting ground to avoid issues with starting your career in Python. Sometimes, installing and failing to understand what’s required can be a real bottleneck to resolve. It’s suggested to undergo such problems early on so that they don’t pester the execution process at the end.

Hopefully, after reading this post on how to run python programs, you must have understood how to run python program in cmd & how to run python program in windows.

With a Python Programming course, you can become a Python coding language master and a very qualified Python programmer. After the course, any aspiring programmer can learn from Python’s basics and continue to master Python.

Register for FREE Digital Marketing Orientation Class
Date: 23rd Jan, 2021 (Sat)
Time: 11 AM to 12:30 PM (IST/GMT +5:30)
  • This field is for validation purposes and should be left unchanged.
We are good people. We don't spam.

You May Also Like…


Submit a Comment

Your email address will not be published. Required fields are marked *