After writing a set of code or a script, we need to execute it so the system can perform all the instructions that are in the script. If we want to define a high-level program or a script, we can say that it is a set of instructions. Whether you are a beginner or advanced python developer , you should know how to execute and run Python scripts or code. By executing your Python script or code, you get to know if it works as intended or not. Well, this article answers how to run a Python script/program using different methods. As a Python developer, it becomes essential for you to be aware of the different ways with which you can execute a Python script or code. So, let's not waste any time and get started!
How to Run a Python Script or Program?
There are various methods to run a Python script. Here is the list of the most common ways to run a Python script:
- Command-Line or Terminal
- Python Interactive Console or Shell
- Text Editors (Specific)
We will discuss each method in detail, but before that let us get a brief overview of a script, Python interpreter, and the requirements for running Python scripts.
What is a Script?
In programming, we use the term script for a file that contains executable code. In simple terms, a script is a text file that 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 executes each command of the script sequentially. If we talk about Python script or Python scripting, we can say that a Python script is a text file containing the Python code, and the extension of that file should be
. To run a Python script, we require a
which can be downloaded from the official website of Python. Check out
how to download Python.
What is a Python Interpreter?
A Python interpreter is a translator software that allows us to execute Python code. When we download Python from its official site, we download the Python interpreter and some of its tools so that we can run Python scripts in our system. It is essential to install Python in your system before you try to run Python scripts because the system does not understand the command and instructions of high-level programming languages by default. To run or execute a high-level programming code we require translators such as compiler and interpreter , Python uses an interpreter as a translator for the execution of its code.
Python in Terminal and Requirement of Python Script
Once you download Python in your system, you get the Python executable shell command on your command-line interface or terminal. On the Python shell, you can write and execute the Python code at the same time. To use the Python standard console or Python shell, you need to open your command-line interface or terminal, which is Command Prompt on Windows and Terminal on Linux and macOS. Type
on your command line or terminal and hit the Enter button. You will see an interactive mode represented by the >>> symbol, and now you can write and execute your Python code here. Here is an example of how to use the Python Terminal Shell. By executing the
command on your command-line interface or terminal, you can access the Python executable shell.
This Python tool comes in handy when we want to run a specific set of code to check its output. By using the Python executable shell, we can write a thousand lines of code and execute them simultaneously. But there is a problem with writing code in the Python terminal shell; once we exit the console, all the code we have written get vanished and that's why we require Python scripts. We create Python scripts so that we can save the Python code in a file with the .py extension, and we can execute it on any platform and at any time.
To exit from the Python Standard Interactive Console, press
and press enter. If you are using Unix Operating System, press
Various Methods to Execute Python Script
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 use a text editor or IDE, such as Sublime, Notepad ++, PyCharm, etc.
1. Run a Python Script in Terminal/Command-Line
To run a Python script in the terminal or command-line interface, your system must have Python installed in it. To execute the Python script in the command line, you first need to navigate to the directory where the file is stored. After that, you need to write Python or Python3 command followed by the full name of your Python script, including extension and hit Enter.
C:\Users\Public\script.py print("Hello world It's Tech Geek Buzz")
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 displayed the output of the
file on the command line, but we can save that output on a text file by using the
python filename > textfile name
This command will create a
file on the same directory where the script file exists and store the output returned by the script execution. However, every time you run this shell command, the new output will overwrite the old output. If you want to add the new output at the end of the previous output, use this command: <
strong>Python script name >> textfile name
This command would create a new
file if the file did not exist, else it will write the output at the end of the old output.
2. Run a Python Script on the Python Interactive Console or Shell
In Python, we have a keyword
which allows us to import a module or other Python files in the current file so that we can use the pre-written code of those files. Also, the import keyword lets us execute a Python script in the interactive Python console. To execute the Python script on the interactive Python console, you need to open your command line or terminal, navigate to the directory where the script is stored, open the interactive console, and use the
to execute the file.
#C:\Users\Public\script.py print("Hello world It's Tech Geek Buzz")
Note: This method will work only when you are in the same directory where the script is stored.
3. Run a Python Script on an IDE
If you are using a Python IDE, you will get a run option that allows you to directly execute your Python code. This is one of the significant advantages of using any IDE. An IDE provides us with an interactive text editor where we can write our Python 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, you first need to open the Python script on the IDE. After that, you need to right Click on the text editor and click on the run option.
4. Run a Python Code on a Text Editor
Text editors are not supposed to execute the code. 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. However, text editors, such as Visual Studio Code, provide plugins to run the code on the code editor itself.
For any Python developer, running Python scripts or code is an everyday task. Knowing how to run a Python script is important because it helps to know about the output of a Python code or program. In this article, we explained the various methods to run a Python script. It does not matter which method you choose as the output of your Python script would remain the same. The command-line and terminal approaches come very useful when you are using a Text editor; otherwise, you can simply execute your Python code on an IDE. In the development phase, we often use the command line and IDE approach, but we do not use the Python shell terminal and import method, because import is only used when we want to import any module in our program.
People are also reading: