{"id":7494,"date":"2026-02-23T17:10:44","date_gmt":"2026-02-23T17:10:44","guid":{"rendered":"https:\/\/commandlinux.com\/?p=7494"},"modified":"2026-03-27T09:42:56","modified_gmt":"2026-03-27T09:42:56","slug":"how-to-run-python-script-in-linux","status":"publish","type":"post","link":"https:\/\/commandlinux.com\/how-to\/how-to-run-python-script-in-linux\/","title":{"rendered":"How To Run Python Scripts In Linux"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"UTF-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n<link href=\"https:\/\/fonts.googleapis.com\/css2?family=Inter:wght@400;600;700&#038;display=swap\" rel=\"stylesheet\">\n<style>\n  \n  pre {\n    background: #f4f4f5;\n    border-left: 3px solid #6366f1;\n    padding: 14px 18px;\n    overflow-x: auto;\n    font-size: 15px;\n    border-radius: 4px;\n    margin: 16px 0;\n  }\n  code {\n    font-family: 'Courier New', monospace;\n  }\n  table {\n    width: 100%;\n    border-collapse: collapse;\n    margin: 18px 0;\n  }\n  th, td {\n    border: 1px solid #ddd;\n    padding: 10px 14px;\n    text-align: left;\n    font-size: 15px;\n  }\n  th {\n    background: #f8f8f8;\n  }\n  a {\n    color: #4f46e5;\n    text-decoration: none;\n  }\n  a:hover {\n    text-decoration: underline;\n  }\n  details {\n    border: 1px solid #e5e5e5;\n    border-radius: 6px;\n    margin-bottom: 8px;\n    overflow: hidden;\n  }\n  details[open] {\n    border-color: #c7c7c7;\n  }\n  summary {\n    padding: 14px 18px;\n    cursor: pointer;\n    font-weight: 600;\n    font-size: 16px;\n    background: #fafafa;\n    list-style: none;\n    display: flex;\n    justify-content: space-between;\n    align-items: center;\n  }\n  summary::-webkit-details-marker {\n    display: none;\n  }\n  summary::after {\n    content: '+';\n    font-size: 20px;\n    color: #666;\n    transition: transform 0.2s;\n  }\n  details[open] summary::after {\n    content: '\u2212';\n  }\n  details p {\n    padding: 4px 18px 14px;\n    margin: 0;\n    color: #444;\n    font-size: 15px;\n  }\n<\/style>\n<\/head>\n<body>\n\n<p>Python is one of the most widely used languages for automation, data analysis, and server management. If you work on a Linux machine or remote VPS, knowing how to run Python scripts in Linux is a must. This guide covers each step, from uploading your file to scheduling automatic runs.<\/p>\n\n<h2>Prerequisites for Running Python Scripts in Linux<\/h2>\n\n<p>Before you begin, gather your VPS login credentials: server IP address, SSH port, SSH username, and SSH password. Your hosting provider&#8217;s control panel lists these under the SSH access section.<\/p>\n\n<h2>How to Run Python Scripts in Linux VPS<\/h2>\n\n<h3>Transfer Your Python File to the Server<\/h3>\n\n<p>To run Python scripts in Linux on a remote machine, upload your code first. The <a href=\"https:\/\/commandlinux.com\/man-page\/man1\/scp\">SCP command<\/a> encrypts both the file and your credentials during transfer.<\/p>\n\n<p>Open your terminal and type:<\/p>\n\n<pre><code>scp \/local\/path\/file.py user@server_ip:\/remote\/path\/<\/code><\/pre>\n\n<p>Replace the placeholders with your actual paths, username, and IP.<\/p>\n\n<h3>Check if Python Is Installed<\/h3>\n\n<p>Most Linux distributions come with Python pre-installed. Verify by typing:<\/p>\n\n<pre><code>python3 --version<\/code><\/pre>\n\n<p>If nothing appears, install it with <a href=\"https:\/\/commandlinux.com\/man-page\/man8\/apt-get\">apt-get<\/a> on Ubuntu or Debian:<\/p>\n\n<pre><code>sudo apt-get install python3<\/code><\/pre>\n\n<h3>Run the Script Using the Interpreter<\/h3>\n\n<p>The fastest way to run Python scripts in Linux is calling the interpreter:<\/p>\n\n<pre><code>python3 script.py<\/code><\/pre>\n\n<p>Python reads your file and prints output to the terminal right away.<\/p>\n\n<h3>Make the Script Executable<\/h3>\n\n<p>You can also run your file without typing python3 each time. Add a shebang line at the top of your script:<\/p>\n\n<pre><code>#!\/usr\/bin\/env python3<\/code><\/pre>\n\n<p>Then grant execute permission and launch it:<\/p>\n\n<pre><code>chmod +x script.py\n.\/script.py<\/code><\/pre>\n\n<p>Without the shebang, Linux treats the file as a shell script and throws a syntax error.<\/p>\n\n<h2>Running Python Code Interactively<\/h2>\n\n<p>Type python3 in your terminal to open an interactive session where you can test code line by line. To exit, type exit() or press Ctrl+D.<\/p>\n\n<h2>Troubleshooting Common Errors When You Run Python Scripts in Linux<\/h2>\n\n<table>\n<tr><th>Error Message<\/th><th>Cause<\/th><th>Fix<\/th><\/tr>\n<tr><td>PermissionError: [Errno 13]<\/td><td>No execute rights<\/td><td>Run chmod +x script.py<\/td><\/tr>\n<tr><td>no such file or directory<\/td><td>Wrong path<\/td><td>Check your folder or use the full path<\/td><\/tr>\n<tr><td>python3: command not found<\/td><td>Not installed<\/td><td>Install via your package manager<\/td><\/tr>\n<tr><td>Script behaves oddly<\/td><td>Wrong version<\/td><td>Specify python3 or python2<\/td><\/tr>\n<\/table>\n\n<h2>Scheduling Automatic Execution with Cron<\/h2>\n\n<p>To run Python scripts in Linux at a set time each day, use cron. Open your crontab<\/a> with:<\/p>\n\n<pre><code>crontab -e<\/code><\/pre>\n\n<p>Add a line like this to run your code daily at 2 PM:<\/p>\n\n<pre><code>0 14 * * * \/usr\/bin\/python3 \/home\/user\/script.py<\/code><\/pre>\n\n<p>Verify your scheduled jobs with crontab -l.<\/p>\n\n<h2>FAQs<\/h2>\n\n<details>\n<summary>How do I check which Python version is installed on Linux?<\/summary>\n<p>Run python3 &#8211;version in your terminal. The system prints the version number. If nothing shows up, Python is not installed.<\/p>\n<\/details>\n\n<details>\n<summary>Can I run Python 2 and Python 3 on the same server?<\/summary>\n<p>Yes. Use python2 or python3 to call the version you need. Both coexist on the same Linux system without conflict.<\/p>\n<\/details>\n\n<details>\n<summary>Why does my script show a &#8220;Permission denied&#8221; error?<\/summary>\n<p>Your file lacks execute permissions. Run chmod +x script.py to fix this, then execute the script with .\/script.py.<\/p>\n<\/details>\n\n<details>\n<summary>What does the shebang line do in a Python script?<\/summary>\n<p>The shebang (#!\/usr\/bin\/env python3) tells the shell which interpreter to use. Without it, Linux fails to parse Python syntax.<\/p>\n<\/details>\n\n<details>\n<summary>How do I run a Python script in the background on Linux?<\/summary>\n<p>Append an ampersand: python3 script.py &#038;. Use nohup python3 script.py &#038; to keep it running after closing the terminal.<\/p>\n<\/details>\n\n<\/body>\n<\/html>\n","protected":false},"excerpt":{"rendered":"<p>Python is one of the most widely used languages for automation, data analysis, and server management. If you work on a Linux machine or remote VPS, knowing how to run Python scripts in Linux is a must. This guide covers each step, from uploading your file to scheduling automatic runs. Prerequisites for Running Python Scripts<\/p>\n","protected":false},"author":1,"featured_media":7496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[],"class_list":["post-7494","post","type-post","status-publish","format-standard","has-post-thumbnail","category-how-to"],"_links":{"self":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/7494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/comments?post=7494"}],"version-history":[{"count":2,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/7494\/revisions"}],"predecessor-version":[{"id":8685,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/posts\/7494\/revisions\/8685"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media\/7496"}],"wp:attachment":[{"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/media?parent=7494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/categories?post=7494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/commandlinux.com\/wp-json\/wp\/v2\/tags?post=7494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}