Linux Commands

How to Run a Python Script in Linux

Python has become a trendy scripting language due to its unique features. Unlike other languages, Python lets you to write the complex programs in a concise and easily readable format. Moreover, it is a non-elusive and easy-to-learn language, and most of its applications fall in the artificial intelligence and machine learning industry.

Furthermore, it’s an object-oriented language that is packed with top-level data structures, dynamic binding, and dynamic typing. You can run it on all the most used systems like Windows, mac, and Linux. However, Linux users are unaware on how to execute the Python scripts on their devices. So, in this short blog, we will briefly explain how to run a Python script in Linux.

How to Run a Python Script in Linux

First, check the version of the currently installed Python in your system.

For Python2:

python –version

For Python3:

python3 --version

You can now run any Python script on your Linux device by merely entering the following given command:

python script_name.py

Make sure that you replace the “script_name.py” with the name of the actual script that you want to execute. For example, let’s run the “hello_world.py” script.

python hello_world.py

This command runs the script and displays the result as shown in the following image:

Moreover, if you want to save this output in a separate text file, use the command as follows:

python3 script_name.py > file.txt
  1. Again, replace the “script_name.py” as you did in the earlier command.
  2. The “>” symbol forwards the resulting output to a text file.
  3. Replace the “file.txt” with the text file where you are saving the output. Remember, it directs the output to the specified file if it already exists in the current directory. Otherwise, it creates a new text file with your specified name to save the result.

For instance, if you want to direct the output to a file named “results.txt”, the command will be:

python3 hello_world.py > result.txt

The command line does not display anything by default when you enter this command. Therefore, to check whether it has created the file, use the “ls” command.

As you can see at the bottom right of the previous image, the system creates the specified text file and stores the script’s output.

Similarly, you can also add the output of other Python scripts to the same file using double “>>” instead of single “>” in the previous command.

python3 hello_world.py >> results.txt

After running the previous command, you will see the two outputs inside the “result.txt” file. The “>>” expression instructs the systems to add/append to a particular text file.

Conclusion

Python scripts refer to the files containing Python codes. Everyone in the programming world needs to learn how to execute the Python programs and scripts in Linux systems. That’s why we explained about running a Python script in Linux in this guide. First, we discussed the command to run the scripts. Then, we demonstrated the methods of saving its output to the text files using simple examples.

About the author

Prateek Jangid

A passionate Linux user for personal and professional reasons, always exploring what is new in the world of Linux and sharing with my readers.