python

How to run a python script from the command line in Windows 10

In this tutorial, we are going to see how to run a python script from the command line in windows 10. Running a Python program is a very easy task after installation. Here are the steps:
 

Python program:
print("Welcome To StackHowTo!")

Output:

Welcome To StackHowTo!
 

How to compile and run the above program
Prerequisites: You must install Python on your system. You can get it on this link.

 
Step 1 : Open a text editor, like Notepad on Windows, and TextEdit on Mac. Copy the above program and paste it into the text editor.

You can also use an IDE like PyCharm to run the python program but to keep things simple, we’ll only be using the text editor and command prompt (or terminal) for this tutorial.
 
Step 2 : Save the file as test.py.
 
Step 3 : In this step we will run the above program. To do this, open the command prompt (CMD) in Windows, or Terminal if you are on Mac OS.

To compile the program, type the following command and click on “Enter”.

python test.py
You may get the following error when you try to compile the program: “python is not recognized as an internal or external command”. This error occurs when the path to Python.exe is not defined in your system.

If you get this error, you must first set the path before compiling.
 

 

Define the path in Windows:

To set the Python environment variable:

1- Go to your computer properties


2- Click on Advanced System Settings
 

 
3- Click on Environment Variables
 

 
 
Under System Variables, scroll down to find the Path variable then click on “Edit”.
 

 
Copy the path to the C:\Python27 folder where the Python executable is located. For python 3 the path to the executable is: C:\Users\(username)\AppData\Local\Programs\Python\Python37
 

 
Click on “New”.
 

 
Add the value C:\Python27.
 

 
Click on the “OK” button of the three windows.

Now you can run your Python script from the command line on Windows 10.
 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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