How To Run A Python Script In Linux

Running Python scripts on a Linux system is an essential task for those who want to learn and use the Python programming language on a Linux-based operating system. In this tutorial, we will guide you through the process of running a Python script in Linux.

Step 1: Check if Python is installed

In most Linux distributions, Python comes pre-installed. However, you may want to verify the installation and check the Python version. To do so, open your terminal and type:

If one of the commands above returns the version number, you have Python installed on your system. If not, you can install Python using your distribution package manager. For example, on Ubuntu-based systems, you can use the apt-get command:

Step 2: Create a sample Python script

Before running a Python script, you need to create one. You can use any text editor like Vim, Nano, or Gedit. In this example, we will create a simple script called hello_world.py that prints “Hello, World!” to the console using the print function. Open your favorite text editor and put the following line of code into it:

Save the file hello_world.py in a directory of your choice.

Step 3: Run the Python script from the terminal

Now that you have created a Python script, it’s time to run it. Open the terminal and navigate to the directory where you saved the script using the cd command:

To run the script, use one of the following commands, depending on the Python version installed on your system:

You should see the following output:

Hello, World!

Step 4: Run the Python script with a specific Python interpreter and external libraries

If your script requires a specific Python interpreter or external libraries, you may need to create a virtual environment. A virtual environment is an isolated Python environment where you can install specific packages without affecting the system-wide Python installation.

To create a virtual environment, first, make sure the virtualenv package is installed:

Then, create a virtual environment and activate it:

Now you can install packages using pip and run your script in the virtual environment:

To deactivate the virtual environment, simply type deactivate in the terminal.

Step 5: Run the Python script at a specified time or as a background process

If you want to schedule your Python script to run at a specific time or to run it in the background, you can use the cron utility or run the script with the nohup command.

To schedule the script with cron, run the following command:

Add a new line with the following format:

For example, to run the script every day at 3:00 PM, you can add the following line:

Save the file and exit the editor.

To run the script in the background using the nohup command, simply run:

This command will run the script in the background and ignore the hang-up signal, allowing the script to keep running even after the terminal is closed.

Full code:

Conclusion

In this tutorial, we have guided you through the process of running a Python script in a Linux environment. Now you should be able to create, run, and schedule Python scripts on your Linux system, using the terminal and various utilities. Make sure to practice these steps and experiment with different script variables and environments to fully understand the potential of running Python scripts on Linux.