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:
1 |
python --version |
1 |
python3 --version |
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:
1 2 |
sudo apt-get update sudo apt-get install python3 |
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:
1 |
print("Hello, World!") |
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:
1 |
cd /path/to/your/script |
To run the script, use one of the following commands, depending on the Python version installed on your system:
1 |
python hello_world.py |
1 |
python3 hello_world.py |
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:
1 |
pip install virtualenv |
Then, create a virtual environment and activate it:
1 2 |
virtualenv my_venv source my_venv/bin/activate |
Now you can install packages using pip
and run your script in the virtual environment:
1 2 |
pip install package_name python hello_world.py |
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:
1 |
crontab -e |
Add a new line with the following format:
1 |
minute hour day month weekday command |
For example, to run the script every day at 3:00 PM, you can add the following line:
1 |
0 15 * * * /path/to/python3 /path/to/hello_world.py |
Save the file and exit the editor.
To run the script in the background using the nohup
command, simply run:
1 |
nohup python3 hello_world.py & |
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:
1 |
print("Hello, World!") |
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.