How to Run Python Code from CMD: Step-by-Step Guide

Running Python code from the command line (cmd) can be an efficient way to quickly execute scripts or interact with the interpreter. Here's a comprehensive guide on how to do this effectively.

minecraft game using python programming
minecraft game using python programming

Before we delve into the details, ensure that Python is installed on your system and added to your system's PATH. If not, you can download it from the official Python website, install it, and ensure it's added to your PATH for easy access.

Run Python in CMD on Windows (Beginner-Friendly Guide)
Run Python in CMD on Windows (Beginner-Friendly Guide)

Running Python Scripts

To run a Python script from the command line, navigate to the directory containing the script using the 'cd' command, then use the 'python' or 'python3' command followed by the script name.

How To Run A Python Script
How To Run A Python Script

Here's an example: If your script is named 'example.py' and located in a folder on your Desktop, you would run it like this:

cd Desktop
python example.py

Specifying the Python Interpreter

30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast

If you have multiple Python versions installed on your system, you can specify which one to use by providing the full path to the interpreter. For instance, if you're using Python 3.8, you might run:

C:\Path\to\python38\python.exe example.py

On Unix-based systems (like Linux or macOS), use:

/usr/local/bin/python3.8 example.py

Running Scripts with Arguments

How to Learn Python Coding Faster? Easy Tips for Beginners
How to Learn Python Coding Faster? Easy Tips for Beginners

You can pass arguments to your script from the command line by including them after the script name. For example:

python example.py arg1 arg2 arg3

In your script, you can access these arguments using the 'sys.argv' list. The first element is the script name, so the first argument would be 'sys.argv[1]'.

Interactive Python Sessions

Python Tip: Use zip() Like a Pro 🐍
Python Tip: Use zip() Like a Pro 🐍

You can also use the command line to interact with the Python interpreter in an interactive session. This can be useful for testing code snippets, trying out functions, or learning Python.

To start an interactive Python session, simply type 'python' or 'python3' into your command line, followed by the Enter key. You'll then be presented with a prompt ('>>>' for Python 3) where you can type and run Python code.

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
an image of a computer screen with cats on it and the words, python list method
an image of a computer screen with cats on it and the words, python list method
Learn and practice Python programming (Python Coding)
Learn and practice Python programming (Python Coding)
Simple Python code for snake game with images
Simple Python code for snake game with images
30-Day Python Learning Roadmap for Beginners | Step-by-Step Coding Plan 2026
30-Day Python Learning Roadmap for Beginners | Step-by-Step Coding Plan 2026
Emojis in python
Emojis in python
a computer screen with the text pass attack on it
a computer screen with the text pass attack on it
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
Top Greedy Algorithm Problems with Easy Python Code
Top Greedy Algorithm Problems with Easy Python Code
the diagram shows how to use python commands for web pages and other contents, including data
the diagram shows how to use python commands for web pages and other contents, including data
Calendar in python Save it ✓
Calendar in python Save it ✓
Game Development with Python Programming...
Game Development with Python Programming...
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
How to Debug Your Python Code
How to Debug Your Python Code
the top 50 python project ideas
the top 50 python project ideas
Python Code to Identify Armstrong Numbers!
Python Code to Identify Armstrong Numbers!
a computer screen with the words'python program to generator password'on it
a computer screen with the words'python program to generator password'on it
How to Check the Version of Python
How to Check the Version of Python

Exiting an Interactive Session

To exit the interactive session, you can simply type 'exit()' or press Ctrl+Z followed by Enter (on Windows) or Ctrl+D (on Unix-based systems).

Using the Interactive Session Effectively

During an interactive session, you can use the built-in help command to get help with Python modules, functions, and other elements. For example, to get help with the 'math' module, you would type 'help(math)'. You can also save all or part of an interactive session to a file by using the 'python -i' flag when starting the session, or the 'shell.writeexecutable(id)' function within the session.

Now that you've learned how to run Python code from the command line, you can integrate this skill into your workflow for greater productivity and code testing convenience.

Related Articles

Send Python Code In Email Python Speech Recognition Example Python Dump Json To File Formatted Speech Recognition Python Code How To Run A Simple Code In Python Gurobi Sample Code Python Code To Send Email Smtp Python Excel Example Code Python Json Save Pretty Random Sample Gamma Distribution Python