How to run your python program from Windows command prompt

How to run your python program from Windows command prompt

11th July 2019 • 13,459 views



Title: How to run your python program on the command prompt

Difficulty Level: Beginner

Approach: Hands-on

Introduction

If you starting out in Python programming, the first thing you will like to know is how to run your Python programs after you have written them; and most times you are even wondering where you can write your Python program. Naturally you will be advised to get an IDE like PyCharm or Eclipse for your Python programs. This is good and efficient, but it does not mean you cannot write and run your Python program without them. I have seen some occasions that will make you want to quickly start writing and running your Python program without an IDE. The reasons include but not limited to:

  • You simply can't get an IDE to install - If you don't have Internet access to download and install an IDE and you can't find someone to give you the raw file to install. This can be one of those times.
  • You are in a Python meetup tutorial ad the IDE every is installing isn't working on your system for some reasons. Then the instructor says, "Let's go ahead without Jane we will look at what is wrong with her system later", and in this case you are Jane!
  • Another occasion is when you want to create an automated process in Python. I once wrote a Python script to change my MAC address and I want this to be happening every 3 minutes. (It was for hacking class). i can't be loading my IDE and clicking "Run" every 3 minutes, so the command prompt was my best option. When you find yourself in any of this situation, you could easily get frustrated and discouraged. This is why I am going to be showing you how you can write and run your Python programs using just the Windows Notepad and the Windows Command Prompt. Have a nice reading

What you will need.

- The Python Interpreter.

Download the latest Python Interpreter from Python.org. The file size is about 10MB and that is all you are going to need. Now being able to run your python program fro the command prompt begins from when you are installing the interpreter. When Installing Python, be sure to check the box that says “Add Python x.x to PATH”

So if you check that box and install your Python, it will automatically be added to your system path so that it can run from the command prompt. But if you did not check that box, then you will have to manually add python to system path.

To do this, you have to navigate to the install location of the Python interpreter. Now for some reason, Python 3 installs in the hidden section of the window. So to see this, you have to enable view hidden files

1. Open the window explorer, click on “organize “ at the top left corner and select “Folder and Search Options”

2. Click on the view tab and enable “Show hidden files, folders and drive”

Once you have done this, navigate to the install path which is usually C:\Users\[yourusername]\AppData\Local\Programs\Python\Python37-32 Now copy that path to the clipboard so that we can add it to the system path.

3, Now we have to open the system environment variable window to paste our python path. To access the system variable path, follow these steps

- Right click on my computer or This PC icon

- Select properties

- Click on advanced system settings

- Select Environment variables

- Under system variables, scroll and get to path, select and click edit

- Add a semi-colon “;” and then paste the python path from your clipboard

Hit Ok on all open dialogs and you have just manually added your python to system path.

Next open up your notepad and write your first Python program. Simply write

Print(“Hello World”)

Then click File-Save As. Give it a simple name like “hello.py”. Be sure to change the file save as type from “.txt” to “all files”. Again I will suggest that you select the Desktop as the save destination just to keep thing simpler as a beginner. If you did this correctly, then you should see the hello.py file on your desktop with the python logo as shown

Then open the command prompt and type python hello.py Hit enter and you should see python display “Hello World” on the console screen

(Please when you open your command prompt, don’t forget to change your directory to the Desktop where you saved your hello.py file by typing cd Desktop first)

Congratulations you have successfully written your python program on notepad and run fro the command prompt. So go ahead and begin learning python and writing your programs without any qualms. I hope you found this article interesting. Fell free to like and share your comment.






Comments

We would like to hear from you to know how you have benefitted from this article. Please feel free to share a comment. (You will need to be logged into your Facebook account to do this)