Run python script from command line

Run python script from command line

Step 1:

In order to run a python script in command line we need a python interpreter that is to be downloaded and installed.

Click here to download Python for Windows.

Click here to download Python for Linux/Unix.

Click here to download Python for Mac OS

Select the python version you want to download (Downloading the latest version is recommended).

Then install the executable file that has been downloaded.

Step 2 :

The next step is to set path variable. To set path follow the following steps:

Go to my computer -> select properties -> select advanced settings -> select environment variable -> new.

In variable name  write path and in variable value copy the path of Python interpreter downloaded.
Then click ok

Now path is set for executing python script.

Step 3 :

First we must have a python file to execute.
Open any text editor (Notepad or Notepad++)

Copy the python code give below in the text editor.

print("Welcome to my first program")

Run python program in command prompt
Save the file with ".py" extension. For example, welcome.py. 
Save the file in any folder.
Run python program in command prompt


Here I have saved the file in a folder named PythonProgram in D drive.

Step 4 :

Open command prompt.
Type "cd.." in order to change the directory.
Run python program in command prompt


Then type the drive name, "D:" for my case.

Run python program in command prompt

Now type "cd foldername".
Run python program in command prompt


You'll now get into the folder.
Now type the python file name with ".py" extension.


Run python program in command prompt

That's it now you will be able to see the output of the python script which you have executed.

Comments

Popular posts from this blog

Finding the percentage in python - HackerRank solution

HackerRank challenges with solutions (Rearrangement, Formula, Word sorting, Numbers, Sentence)

ginortS in Python - HackerRank solution