How to Run Python Scripts in Linux: A Comprehensive Guide
Running Python scripts in Linux is a fundamental skill for developers and system administrators. Python is a versatile and easy-to-use programming language that is widely used for automating tasks, analyzing data, building applications, and managing systems. In this tutorial, we will explain how to run a Python script in your Linux server’s command line and troubleshoot common errors that may occur.
Prerequisites
Before we begin, make sure you have the following details of your VPS (Virtual Private Server) ready:
- Server IP address
- SSH port
- SSH username
- SSH password
If you are a Hostinger user, you can find these details under VPS → Server Details → Manage. Your credentials are located under the SSH access tab.
How to Execute a Python Script in your Linux VPS Command Line
This section will walk you through the steps to run Python scripts in the Command Line Interface (CLI) of your Linux VPS, from transferring the script to your VPS to viewing logs.
1. Transfer a Python Script to VPS
To run your script, you will need to import it from your local machine to your VPS. There are several ways to do this, such as using Secure Copy Protocol (SCP), rsync, SFTP (SSH File Transfer Protocol), or Secure Shell (SSH) protocol. Each method offers a different approach to transferring data with varying levels of complexity and security.
Here are the steps to transfer a Python script using SCP:
- Connect to your VPS using PuTTY or a similar SSH client.
- Launch the PuTTY SSH client and enter your server’s SSH IP and SSH port.
- Enter your SSH username and password to continue.
- Use the SCP command to transfer the file from your local machine to your VPS. For example, the command would look something like this:
scp /path/to/local/file username@IP_address:/path/to/destination
Make sure to replace /path/to/local/file
with the path to your local Python script, username
with your SSH username, IP_address
with your VPS IP address, and /path/to/destination
with the desired location on your VPS.
Repeat this step if you need to transfer multiple files or entire folders to your VPS.
2. Make Sure Python Is Installed on Your VPS
Before running a Python script on your VPS, make sure Python is installed. Most Linux distributions come with Python preinstalled, but if it is not installed on your VPS, you will need to install it manually. To check if Python is installed, run the following command:python --version
or python3 --version
If Python is not installed, you will need to install it using the package manager specific to your Linux distribution. For example, on Ubuntu/Debian, you can use the following command:sudo apt-get install python3
3. Make Your Script Executable
Before executing a Python script, you need to make it executable. To do this, add a shebang line at the top of your script. The shebang line tells the VPS which interpreter to use to run the script. For example, add the following shebang line for Python 3:#!/usr/bin/env python3
After adding the shebang line, save the script and make it executable using the following command:chmod +x script.py
4. Run the Script and View it in the Terminal
Once the script is executable, you can run it in the terminal using the following command:./script.py
The script will be executed, and you will see the output directly in the terminal.
5. View Logs
You can view the system logs to check for errors or messages generated by the script. Linux logs provide valuable information for troubleshooting issues, monitoring system health, diagnosing problems, and investigating security incidents. Common log files include syslog, auth.log, kern.log, and more. To view log files, navigate to the appropriate log directory and use commands like less
or tail
to view the contents.
Running a Python Code Interactively
Python provides an interactive mode where you can execute code blocks or single lines of code and receive immediate feedback. This mode is useful for testing code snippets or experimenting with Python features.
1. Launching Python’s Interactive Mode
To launch Python’s interactive mode on your VPS, type python
or python3
into the terminal to launch the Python interpreter.
2. Executing Python Commands Interactively
In interactive mode, you can enter Python commands directly and see their output immediately. This mode is useful for testing code snippets or experimenting with Python features.
3. Exiting Interactive Mode
To exit Python’s interactive mode, type exit()
or press Ctrl + D on your keyboard.
Troubleshooting Common Execution Errors
Encountering errors while running Python scripts is common. Here are some common errors you may encounter and how to troubleshoot them:
Permission Denied Error
Error message: PermissionError: [Errno 13] Permission denied
Cause: The script does not have execute permissions.
Troubleshoot: Use the chmod +x script.py
command to grant execute permissions to your script.
No Such File or Directory Error
Error message: no such file or directory: 'script.py'
Cause: The script file path is incorrect.
Troubleshoot: Make sure you are in the correct directory or provide the full path to the script when running it.
Python Version Compatibility
Cause: You have multiple versions of Python installed on your Linux system, and your script requires a specific version to run correctly.
Troubleshoot: Specify the desired Python version explicitly when running the script. For example, use python3 script.py
or python2 script.py
depending on your requirements.
Interpreter Not Found Error
Error message: bash: python3: command not found
Cause: The Python interpreter is not installed or not in the system’s path.
Troubleshoot: Install Python using the package manager specific to your Linux distribution and make sure it is added to the system’s path.
Conclusion
Running Python scripts in Linux is a powerful way to automate tasks, analyze data, build applications, and manage systems. By following the steps outlined in this tutorial and troubleshooting common errors, you can smoothly execute Python code on your Linux VPS. We hope you found this article helpful!
How to Run a Python Script in Linux FAQ
Here are some frequently asked questions regarding running Python scripts in Linux:
How Do I Run a Python Executable in Linux?
To run a Python executable in Linux, navigate to the executable’s directory and run it using the ./
prefix followed by the filename. For example, if your executable is named script.py
, you would run it using the command ./script.py
. After running the script, you will see the output in the terminal.
What’s the Difference Between Running Python Scripts Interactively and From the Command Line?
Running Python scripts interactively allows you to execute Python commands line by line in real time, whereas running scripts from the command line allows you to execute the entire script at once. Interactive mode is useful for testing code snippets, while running scripts from the command line is more suitable for executing larger chunks of code.
How Can I Run a Python Script at Scheduled Times on Linux?
To run a Python script at scheduled times on Linux, you can use the cron job scheduler. Create a cron job by editing the crontab file using the command crontab -e
. Specify the schedule and the command to run the Python script. For example, to run the script every day at 2 PM, you would add the following line to the crontab:0 14 * * * /path/to/python3 /path/to/script.py
Save the crontab file, and the script will be executed according to the specified schedule. You can verify the cron jobs using the command crontab -l
.
Esther Kumi
Esther is a copywriter for SaaS and Tech companies. She has a background in engineering and software development. When she’s not writing, she’s conducting creative experiments, inventing systems, writing screenplays, or laughing at her own dad jokes. Follow her on LinkedIn.
👉
Start your website with Hostinger – get fast, secure hosting here 👈
🔗 Read more from MinimaDesk:
- How Many WordPress Plugins Are Too Many? Best Practices for Performance Optimization
- How to Fix Broken Permalinks in WordPress: A Step-by-Step Guide
- WooCommerce vs BigCommerce: A Detailed Comparison for WordPress Users
- How to Use Shortcodes in WooCommerce: A Complete Guide
🎁 Download free premium WordPress tools from our Starter Tools page.