Table of Contents
Python is not just one of the leading programming languages, but also a top choice for dealing with big data and data science projects. The fact that it is among the easiest languages to learn makes the high-level, interpreted, general-purpose programming language even more lucrative.
For using Python on a system, the user first needs to install the Python environment. It will also install the Python interpreter, which is responsible for carrying out Python code execution. It is possible to run Python code directly from the terminal using the Python interpreter.
How to Run a Python Script?
Though it is fast and convenient to use the Python interpreter directly from the terminal, the approach is impractical for real programs. This is because you need to type the code every time it needs to be executed. This is where Python scripts come in handy.
A Python script is simply a Python program, a set of instructions, kept in a text file. Once a Python script has been created, it is possible to execute it over and over again without the need to retype the code included. They also allow editing.
To run a Python script, you need to first set up Python and ensure that you have a working Python interpreter.
The Python Interpreter
The Python interpreter is responsible for executing the Python scripts. It works on the REPL environment, which is also known as an interactive top-level shell.
REPL stands for Reading the command, Evaluating and executing the command, Printing the output, and Looping back and repeating the entire process. The Python interpreter continues execution until instructed to stop by the exit() or quit() command.
How to Start the Python Interpreter?
The most common way of starting the interpreter is to open the terminal and then using the interpreter from the command line. To open the terminal:
- Search for Command Prompt or Powershell in Windows
- Search for Terminal in Linux/MacOS
After the terminal is open, type python and press enter key to start the Python interpreter. The >>> on the terminal represents interaction with the Python interpreter. In case it is not there, you might want to reinstall Python on your system.
Running the Hello, World! Python Program
Let’s start with the traditional Hello, World! program. In front of the >>> prompt, type:
print (“Hello, World!”) Now, hit the enter key. The output will appear as: >>> print (“Hello, World!”)
Note: – While running the Python code, the Python interpreter accounts for the indentation. Hence, if you are adding an extra space just before the print keyword, you will get an error like this:
>>> print("Hello, World!") File "<stdin>", line 1 print("Hello, World!") ^ IndentationError: unexpected indent
Creating the Hello, World! Python Script Using a Text Editor
Using Python interpreter is good enough for quickly executing some Python code. Because a bigger project requires much more lines of code, it is desirable to create a Python script rather than typing it over and over again.
To create a Python script, simply open a text editor on your system and add the Python code that you wish to use. For demonstration, we’ll add the Hello, World! code that we ran earlier to the newly created text file, which is:
print (“Hello, World!”)
Now, save the file as hello.py.
Note: The Python script doesn’t necessarily need an extension. This means that the Python interpreter will run a file containing Python script regardless of what the extension of the file is.
Giving the Python script file a .py extension is for convenience. It makes the file(s) containing Python script easily identifiable.
Running the Hello, World! Python Program Using a Python Script
Now, we will be executing the Python script that we created in the previous section. To start, fire up the terminal. Navigate to the location where the file is stored. The Python script can be easily executed by specifying the filename as a command line argument to the Python interpreter:
The output will be:
You can run any Python script directly from the terminal using this very procedure.
Running the Hello, World! Python Script Using IDLE
Since the 1.5.2b1 implementation version of Python, the Python environment is accompanied by an integrated development environment known as IDLE. It is available as an optional part of the Python packaging. IDLE is written in Python and Tkinter, Python’s de facto GUI.
Other than running Python scripts directly from the terminal, they can also be executed using IDLE. Here are the step-by-step instructions on executing a Python script using IDLE:
- Step #01 – Run IDLE. It will open a “Python Shell” window along with a >>> prompt
- Step #02 – Click on the File tab and then New Window. A new Untitled window will open. Here you can edit a Python script
- Step #03 – Enter the Python script, print (“Hello, World!”), in the Untitled window
- Step #04 – Now, go to the Run tab and then click on the Run Module option. Alternatively, you can press F5 to execute the Python script instantly
- Step #05 – Before displaying the output, you will be prompted with a dialog “Source Must Be Saved. OK to Save?” Click OK to continue.
- Step #06 – In the Save As dialog, specify the directory where you wish to save the Python script and a name for the Python script before hitting the Save button
- Step #07 – Now, the “Python Shell” window will display the output (Hello, World!) for the Python script
- Step #08 – You can edit the Python script and re-run the modified code as many times as required
Running the Hello, World! Python Script Using a Python IDE
IDEs make life easier for programmers. Because Python is a popular programming language, there is an abundance of IDEs.
Different Python IDEs have different interfaces. Hence, the procedure for executing a Python script will be different. However, one thing that each and every Python IDE has in common is convenience.
No matter what Python IDE you are using, it is very easy to create, run, edit, and save a Python script. For demonstration, we’ll be using Eclipse here.
Note: Though Eclipse is one of the most popular IDEs for Java, it can operate as an IDE for a variety of programming languages. In order to use Eclipse for working with Python, you need to install the PyDev plugin.
Creating a Python Script
There are two ways of creating a Python script using Eclipse. Either you can go to File->New->Pydev Module, else you can right-click on a folder (the one added to the PYTHONPATH when setting up the project) in your project and choose, New-> Pydev module.
There will be four fields in front of your right now:
- Source Folder – The folder where the Python script will be stored. Enter the Python script destination here
- Package – It is an additional tool that can organize the code. Leave this blank
- Name – Add a name for the module here. Don’t add the .py extension as it will be added automatically by the IDE
- Template – Leave it with the default value
Now, click on the Finish button to let the Eclipse create a new Python module.
Editing a Python Script
To edit a script, open it in the Project section and double click on it. You can have multiple Python scripts open simultaneously using Eclipse.
Furthermore, it is also possible to open the same Python script in multiple windows. This allows working on several sections of a long Python script as well as making debugging easier.
Running the Hello, World! Python Script
Add the following code to the newly created module:
print (“Hello, World!”)
Now, right-click on the editor window and select Run As->Python Run. The following output will be displayed on the console:
There are several ways to run a Python script. Using the Python interpreter directly from the terminal is suitable when the script is small or while exploring the features and functionality of Python. However, using an IDE for running a big and complex Python script is the go-to option.
In order to keep up with the ever-changing programming domain, you need to keep learning and getting better with your programming skills. You can learn a lot about Python script and other concepts pertaining to Python programming languages with these 10 best Python books.
People Also Read: