How to run a Python Script?

By | May 16, 2020
How to run a Python Script

After writing a set of code or a script, we need to execute it, so the system can perform all the instructions which are written by the user. If we want to define a high-level program or a script, we can say that it is a set of instructions written by the developer, which require a translator so the system can perform all the valid instructions. It does not matter if you are a beginner or advanced python developer you should know how to execute and run the script and code, that you have written. By only executing your code and script, you get to know if your script and code work at all.

Here is the list of ways you can run or execute your Python Script

Vamware
  • By using Command Lines or Terminal.
  • By using Python Console mode.
  • By using any IDE.
  • Text Editors (Specific)

What is a Script?

In programming, we use the term script for a file which contains an executable code; in simple term, a script is a text file which includes a specific program. To execute or run a script we generally require a translator, and it could either be a compiler or interpreter which execute each command of the script sequentially.

If we talk about Python script, we can say that a Python script is a text file containing the python code, and the extension of that file is .py. To run a python script, we require a python interpreter which can be download for various platforms from the official website of Python.

What’s a Python Interpreter?

The Python interpreter is a translator software, which allows us to execute the python code. When we download Python from its official site, basically we download the python interpreter and some of its tools so that we could run our python scripts.

It is essential to install Python in your system before you try to execute any python file or script because the system does not understand the command and instruction of high-level programming languages. To run or execute a High-Level programming code we require translators such as compiler and interpreter, Python uses interpreter as a translator for the execution of its code.

Python Interactive Console and Requirement of Python Script

Once you download Python in your system, you get the Python Interactive console on your Command line or terminal. On the Interactive console, you can write and execute the python code line by line at the same time.

To use the Python standard console, you need to open your Command line or terminal interface, which is Command prompt on Windows and Terminal on Linux and macOS. Type Python or Python3 on your command line or terminal and hit the Enter button. Now you will see an interactive mode represent by >>> symbol, and now you can write and execute your python code here.

Output

This Python interactive tool comes very handily when we want to run a specific set of code to check its output. Using this python tool, we can write a thousand lines of code and execute them simultaneously, but here the main problem is once we exit the console all the code we have written get vanished too, that’s why we require python scripts. We create python scripts, so the code gets saved in a text file, and we could execute it on any platform and any time.

Note: To exit from the Python Standard Interactive Console press CTRL + Z and press enter, if you are using Unix Operating System Press CTRL + D.

Execute Python Script Using Various Methods

To execute a Python script first, you need to create a python file with extension .py, and save it on your local system. To create a python script, you can either use a text editor or IDE such as sublime, notepad ++, notepad, PyCharm, etc.

How to Execute or Run Python Script using Command-Line?

To run a Python Script using Command Line your system must have Python installed in it. To execute Python script on the command line, you first need to navigate to the directory where the file is stored then using the Python or Python3 command followed by the file name including the file extension you can run that specific script.

#C:\Users\Public\script.py
print("Hello world It's Tech Geek Buzz")

Save or Redirect the Output

Save or Redirect the Output:

Python allows us to save the result of the script in a text file, for instance in the above example we just show the output of the script.py file on our command line, but we can save that output on a text file by using the python filename > textfile name command.

Save or Redirect the Output - 1

This command will create a result.txt file on the same directory where the script file exists and store the output return by the script execution, but every time you run this shell command, the new output will overwrite the old output. If you want to add the new output to add at the end line of the past output then use this command Python script name >> textfile name.

Save or Redirect the Output - 2

This command would create a new result.txt file if the file did not exist, else it will write the output at the end of the old output.

How to Execute the python script on the Python Interactive console or shell?

In Python, we have a keyword import which is basically used to import a module or other python files in the current file so the user can use the pre-written code of those files. This import keyword also can be used to execute a python script in the interactive python console.

To execute the python script on the interactive python console first you need to open our command line or terminal, navigate to the directory where the script is stored, and there via using python command you need to open the interactive console, then using the import script_name you can execute the file.

Example:

#C:\Users\Public\script.py
print("Hello world It's Tech Geek Buzz")

python script on the Python Interactive console

Note: This method will work only when you are in the same directory where the script is stored.

How to Execute the Python script on an IDE?

If you are using any IDE which is specially designed for Python then there you will get a run option from where you can directly execute your python code. This is one of the significant advantages of using any IDE; they provide us with an interactive text editor where we can write our python code or script, and they also provide an interpreter or run option via which we can run our code.

PyCharm is a very popular Python IDE, and it comes with an inbuilt python interpreter, code editor and debugger which can be used to write and execute any python script.

To execute the Python script on any IDE, first, open the script file on the IDE, Right Click on the text editor and click on the run option.

Execute the Python script on an IDE

Output:

Execute the Python script on an IDE - Example

How to Execute the Python code on a Text Editor?

Text editors are not supposed to execute the code, and they only provide basic features such as code editing and debugging, for the execution process we have to depend on the Command Line or terminal way of execution. However, there are some text editors such as Visual code Editor which provide plugins which allow the user to run or execute the code on the code editor itself.

Conclusion:

How to execute a python script is a basic and the most important skill every python developer should have, because only by running a script you come to know about the output result and the functionality of your program. It does not matter which method of execution you are using the output of your script would remain the same. The command line and terminal approach come very useful when you are using a Text editor; otherwise, you can simply execute your python code on your Python IDE. In the development phase, we often use the command line and IDE approach, but we do not use the Interactive Console import method, because import has its usages, it only used when we want to import any module in our program.

If you like this article or have any suggestions related to it, please let us know by filling the comment box.

You might be also interested in:

Leave a Reply

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