Home Python How To Run Python Scripts : The Definitive Guide

How To Run Python Scripts : The Definitive Guide

0

Before you can actually start doing all sorts of amazing things with python, you need to know how to run python scripts. So, this is what this article is all about.

Hello Guys,

Welcome to this new article on how to run python using various different ways. In this article, as the name suggests, we will learn and actually see how one can run python scripts or anything related to python.

There are actually multitudes of options out there that can be used to run python files and we will be talking about each one of them in this article.

In particular, we will be learning the following different sections:-

  1. How to run python code (commands) in python IDLE shell?
  2. How to run python scripts using IDLE?
  3. How to run python scripts using the command line?
  4. How to run python code in command prompt shell?
  5. How to find the python path?
  6. How to add the python path as an environmental variable in the user variable list?
  7. And many more.

Run Python Scripts Using Shell

I hope all of you have already installed python IDLE on your system and if you have not, please first install the Python IDLE, and then you can continue with this article. You can read our article How To Install Python on Windows 10.

Once the python has been installed, the next step is to actually see how to run python from the shell and run python by creating a script on python IDLE.

First, on your windows, go to the lower left side and from the search bar, search for “IDLE” and select the appropriate python version that you would like to use. It would look something like….

How To Run Python Scripts 1

Once you have successfully selected the program, Python shell will open and it will resemble something like….

Now, let us briefly go over a few components that are getting displayed on the above screen.

Firstly, we notice the name of the python version and then the shell name associated with it.

Secondly, then we have some 6 menu bars namely “File”, “Edit”, “Shell”, “debug”, “Options”, “Windows”, and “Help”.

Then, we have some information about the current python version that we are using. This starts by saying “Python 3.8.5 (tags/v3…..”.

Finally, the most important thing that I want all of you to notice is the symbol “>>> ” which symbolizes that “wait for the input”. Here is where we can enter our python command in an interactive mode.

What do we mean by running the python code in an interactive mode?

In an interactive mode, the desired python code can be run and the result of that piece of code can be seen immediately. This interactive mode is called python shell.

Okay, we have done much talking, now it is time to actually see how this interactive mode works?

Let us run some python code in this interactive mode. First let us print some simple numbers, integers, floats, strings, list, and dictionaries line by line.

How To Run Python Scripts

We are not limited to just executing one line at a time. We can run multiple lines also. We can define functions, execute if-else statements, loops etc in an interactive mode.

Let us one by one take an example of each of these examples one by one.

Let us run an If-else statement in an interactive mode……

I hope you are able to understand the above piece of code. We defined a variable and then used an if-else block to check whether this value is greater than 100 or not.

Now, let us define a function that calculates the square of the input number in an interactive mode shell…..

Here, we first define the function named “square” which takes on an argument called “x” (the required number) and then computes a variable called “sq” which is x raised to the power 2 and then finally returns that number.

Similarly, let us use the looping in an interactive shell.

How To Run Python Scripts

Here, we have first defined a list of names variables that store some random bunch of names and then loop through that list, which is printed one by one. This is all that we have to talk about in running python using a python interactive shell environment. Now, let us talk about how the script works.

Run Python Scripts From IDLE Script

One question can immediately arise: what is a script?

A python script is just a fancy word for saying a text file with python code in it. In an interactive mode, we get to see the results of each execution immediately while for the script, the results are shown once that file (with python code in it) is run.

Let us see how we can run a python script using IDLE. To create a script, first, open the python IDLE interactive shell.

Once you have the shell opened, you can then navigate to “FILE” and from there choose New File. Alternatively, you could have chosen to use a shortcut called “CNTRL + SHIFT + N”.

A new text editor type window will pop up. The only difference is that this window is specifically designed to write python code which will be clearly visible once you start writing python code in it. As the python keywords will it’s color. Let us write the following code in it….

The difference between code editor and the interactive shell will be immediately visible. Unlike interactive shells, code editors do not have that “>>> “ symbol.

Also, we need to save this file in a proper python extension. Go to File and choose Save as and a new window will pop up.

Enter an appropriate name for the file. Like this….

And once saved, the script window will look something like the following:

There is not much change except that the highlighted field is now “first.py” instead of “Untitled”.

Now, it is time to run this particular python file (script). To run the file, go to Run and choose the Run module option.

After successful execution of the script, the results will be displayed in the IDLE interactive shell. Here is what the result will look like…

With this discussion, we have learned how to run a python script with Python IDLE that comes when we install it. Now, let us look at some other alternatives like command line.

Run Python Scripts From The command line

Now, let us see how we can run python from our command line. There are two different scenarios here.

  1. The first is where your python path is set as an environmental variable in your machine.
  2. Second, when the python path is not set as an environmental variable in your machine.

We will learn how to run the python script or simply python in both the scenarios. Let us first make sure whether the python path is set as an environmental variable or not.

To see this, first we would need to open the command prompt by heading to the lower left of the windows search bar and searching for the command prompt application and then the following screen (black) will be displayed.

Then, once you hit enter to open the command prompt, the following black screen will be displayed…

To check whether python is added to the path or not, write python to the command prompt, if it is, you will see an interactive shell just like you did when you opened IDLE directly.

Here is what it will look like….

To go out of the python interactive shell, you can enter the quit() command and it will safely take you out of the interactive shell and bring you to the command prompt again.

In the interactive shell, you can just enter the code in the similar manner as you did in the IDLE interactive shell. Just to give you an example, here is the same if-else block that we executed in the IDLE earlier.

Okay, now let us see how we can run python scripts which are already there on our computer using the command line.

To show you guys this procedure, I would first need a script. So, let me use the same script that we created using IDLE which we called first.py. Let us look at that script once again.

Now, let us run this same script using command prompt. So, to do that, let us first open the command prompt and navigate to the directory where the python file exits on our system.

If you notice the yellow highlighted area, there you will see that this file exists on desktop.

Let us quickly navigate to this folder.

Here, first I navigated to the desktop and then I used windows command prompt command called “DIR” for printing all the files that are present on the desktop, and from the output list, we can clearly see that there is a file called first.py.

Now, to run this python file from the command prompt, we will type the file name followed by python and hit enter. The required command and the result that we will get is displayed below….

So, with this, we have discussed the first way by which python command and python script can be run. Remember one thing that throughout this discussion, we have assumed that python path was stored as an environmental variable in the window system.

Why do environmental variables need to be updated when python is not recognized from the command line?

It turns out that when you install python then python does it’s best to give you all the necessary instructions to run python correctly. When a new python installation is started, then the welcome screen looks something like the following…..

Here, python gives us this option to check the field which says “Add Python 3.8 to PATH”. There can be two scenarios here…

1. If you check this field, then python path will automatically be added as an environmental variable in your user variables. So, that you will not have to add anything else from your side, Python will work just fine without doing anything.

2. Now, the second scenario is where you choose not to check here or maybe you forgot to check it. Then, you need to follow the instructions given below to update the python path as an environmental variable.

Now, suppose you do not remember whether you checked it or not while installing python, then one of the simplest ways to figure out whether you have installed it or not, is to fire up your command prompt and then type “python” in it and see if it throws any error.

If it does throw any error, that would imply that python is not set as the path in environmental variables,

Why are environmental variables needed?

It turns out that any executable program you install on your system and if you want to access it from the command line, then you will have to add its path as an environmental variable so that it can be recognized when you call it from the command line.

So, I hope with this small discussion, it is clear why is there a need for environmental variables?

Now, let us get right into how one  can set their system’s user variable.

Now, let us consider another scenario where the python path is not there as an environmental variable. If it is the case, then we just need to update it by following some steps which we will be discussing in a while and then once that path is set, then we can execute all the above steps that we have just discussed without having any issues.

So, at this point the only concern is to somehow update the python path in our machine. Let’s do that.

First, we need to figure out where the python is installed on our system. There are two ways to know where the python might be installed on your system.

The first way is just to write it down while installing the python but if you forgot to pay attention to it, then you can use the following procedure.

Open IDLE and execute the following commands and it will give you an idea of where python is installed on your system…

Here, we have first imported a pre-built library called “sys” which stands for system and gives access to system-specific python function and then we called a property of sys module called path which gave us a list of paths.

From here, I can see that the highlighted region gives us information about the path of the python.

Let us navigate to that folder. After navigating to that folder, here is what we see…..

Here in this folder, we can see a file called “python.exe” which is the required file that we need to find and add the corresponding folder to the environmental variables, so that from the command line, this file (python.exe) can be accessed.

Let us first copy the path that python.exe is in. We can do that from….

Once copied the folder, we are ready to paste it at the required location.

Now, go to the search bar (lower left in windows) and search for environmental variables, and it will look something like the following…..

Click it and open it. It might ask you for your administrator’s access. The following pop up for system variables will be opened.

Click on Environmental Variables and it will once again open a new dialog box.

The above window is splitted into two different types of variables. The first one is called “User” variables and the second one is called “System” variables. There will be a variable name called “Path” in both the screens.

Let us first discuss what path means and what is the difference between system and user variables.

The user variables are those variables which will be applicable to a particular user while the system variables are for larger applications which have an effect on the entire system as a whole.

We just need to change the path of the user variable. You can change the system path as well but that’s not required as it is only required where you want to have a system-level effect. We just want our python to be run and that’s it.

Okay, now that we know the difference between system and user variable, let us see how to change the path of user variable.

Double click the highlighted Path variable and then you will see a screen that will look like….

It lists all the path variables that are there as a part of the user variable. Now, to add an entry for the python path, click on New and it will start a new entry in the list and paste the path we just copied from the python directory. It should look something like…

Here, we have just added our python path as an environmental variable in our system. Now, just click ok and apply wherever applicable and you can use python from your command prompt.

So, all the things in python that were earlier in the command prompt can be done now that you have updated your python path and you will be able to access the python from the command line.

Now, after this step is successful, you will need to go back to the initial step when we started our discussion on running python from the command line and perform all the steps mentioned there.

Since, python is now added as a user variable, you will not be having any difficulty in executing those steps.

Now it’s time to explore our How To Run Python Scripts video tutorial guide. 🙂

Okay, so, With this discussion, I will stop this article right here and I hope you have been able to understand everything that we discussed in this article.

Thank you. 🙂