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 in Linux

Before you begin, gather your VPS login credentials: server IP address, SSH port, SSH username, and SSH password. Your hosting provider’s control panel lists these under the SSH access section.

How to Run Python Scripts in Linux VPS

Transfer Your Python File to the Server

To run Python scripts in Linux on a remote machine, upload your code first. The SCP command encrypts both the file and your credentials during transfer.

Open your terminal and type:

scp /local/path/file.py user@server_ip:/remote/path/

Replace the placeholders with your actual paths, username, and IP.

Check if Python Is Installed

Most Linux distributions come with Python pre-installed. Verify by typing:

python3 --version

If nothing appears, install it with apt-get on Ubuntu or Debian:

sudo apt-get install python3

Run the Script Using the Interpreter

The fastest way to run Python scripts in Linux is calling the interpreter:

python3 script.py

Python reads your file and prints output to the terminal right away.

Make the Script Executable

You can also run your file without typing python3 each time. Add a shebang line at the top of your script:

#!/usr/bin/env python3

Then grant execute permission and launch it:

chmod +x script.py
./script.py

Without the shebang, Linux treats the file as a shell script and throws a syntax error.

Running Python Code Interactively

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.

Troubleshooting Common Errors When You Run Python Scripts in Linux

Error MessageCauseFix
PermissionError: [Errno 13]No execute rightsRun chmod +x script.py
no such file or directoryWrong pathCheck your folder or use the full path
python3: command not foundNot installedInstall via your package manager
Script behaves oddlyWrong versionSpecify python3 or python2

Scheduling Automatic Execution with Cron

To run Python scripts in Linux at a set time each day, use cron. Open your crontab with:

crontab -e

Add a line like this to run your code daily at 2 PM:

0 14 * * * /usr/bin/python3 /home/user/script.py

Verify your scheduled jobs with crontab -l.

FAQs

How do I check which Python version is installed on Linux?

Run python3 –version in your terminal. The system prints the version number. If nothing shows up, Python is not installed.

Can I run Python 2 and Python 3 on the same server?

Yes. Use python2 or python3 to call the version you need. Both coexist on the same Linux system without conflict.

Why does my script show a “Permission denied” error?

Your file lacks execute permissions. Run chmod +x script.py to fix this, then execute the script with ./script.py.

What does the shebang line do in a Python script?

The shebang (#!/usr/bin/env python3) tells the shell which interpreter to use. Without it, Linux fails to parse Python syntax.

How do I run a Python script in the background on Linux?

Append an ampersand: python3 script.py &. Use nohup python3 script.py & to keep it running after closing the terminal.

Willie has over 15 years of experience in Linux system administration and DevOps. After managing infrastructure for startups and enterprises alike, he founded Command Linux to share the practical knowledge he wished he had when starting out. He oversees content strategy and contributes guides on server management, automation, and security.