How to Run a Python Script in Linux
Running Python scripts in Linux is a common task for developers, students, and tech enthusiasts. If you're new to Linux or Python, you might wonder how to get your script up and running smoothly. Don't worry — I’ll guide you through the process in a clear, easy-to-follow way.
Whether you want to run a quick script or automate tasks, Linux offers several ways to execute Python code. By the end of this article, you’ll know how to run Python scripts using the terminal, make scripts executable, and troubleshoot common issues. Let’s dive in and make your Python scripts come alive on Linux!
Understanding Python on Linux
Before running a Python script, it’s important to know which Python version is installed on your Linux system. Most Linux distributions come with Python pre-installed, but the version might vary.
- Check Python version: Open your terminal and type
python3 --versionorpython --version. - Python 2 vs Python 3: Python 3 is the current standard. Python 2 is outdated and no longer supported.
- Installing Python: If Python isn’t installed, you can install it using your package manager. For example, on Ubuntu, use
sudo apt install python3.
Knowing your Python version helps you run scripts correctly and avoid compatibility issues.
Running a Python Script Using the Terminal
The most straightforward way to run a Python script in Linux is through the terminal. Here’s how:
- Open the Terminal: You can usually find it in your applications menu or press
Ctrl + Alt + T. - Navigate to Your Script’s Directory: Use the
cdcommand to change directories. For example,cd ~/Documents/python-scripts. - Run the Script: Type
python3 script_name.pyand press Enter.
This method runs the script using Python 3. If your script requires Python 2, replace python3 with python.
Example
Suppose you have a script named hello.py with this content:
print("Hello, Linux!")
To run it:
cd ~/Documents/python-scripts
python3 hello.py
You should see the output:
Hello, Linux!
Making a Python Script Executable
You can also run your Python script like a regular program by making it executable. This saves you from typing python3 every time.
Steps to Make a Script Executable
- Add a Shebang Line: At the top of your Python script, add this line:
#!/usr/bin/env python3
This tells Linux to use Python 3 to run the script.
- Change File Permissions: Use the
chmodcommand to make the script executable:
chmod +x script_name.py
- Run the Script Directly: Now you can run the script by typing:
./script_name.py
Why Use the Shebang?
The shebang line ensures your script runs with the right Python interpreter, even if you have multiple versions installed.
Running Python Scripts with Arguments
Sometimes, your Python script needs input parameters. You can pass arguments directly from the terminal.
How to Pass Arguments
Suppose your script greet.py looks like this:
import sys
name = sys.argv[1]
print(f"Hello, {name}!")
Run it by typing:
python3 greet.py Alice
Output:
Hello, Alice!
Tips for Using Arguments
sys.argv[0]is the script name.- Arguments start from
sys.argv[1]. - Use libraries like
argparsefor more complex argument parsing.
Running Python Scripts in the Background
If you want your script to run without keeping the terminal open, Linux allows running scripts in the background.
Methods to Run in Background
- Use
&at the end of the command:
python3 script_name.py &
- Use
nohupto keep the script running after logout:
nohup python3 script_name.py &
- Use
screenortmuxto create detachable terminal sessions.
These methods are useful for long-running scripts or servers.
Troubleshooting Common Issues
Running Python scripts in Linux is usually smooth, but sometimes you might face problems. Here are common issues and how to fix them:
Python Not Found
If you get an error like command not found: python3, Python might not be installed or not in your PATH.
- Install Python using your package manager.
- Check your PATH environment variable.
Permission Denied
If you get Permission denied when running an executable script:
- Make sure you used
chmod +x script_name.py. - Run the script with
./prefix.
Syntax Errors
Python syntax errors usually mean your script has a typo or uses features not supported by your Python version.
- Check your Python version.
- Review your code carefully.
Module Not Found
If your script imports a module but fails:
- Install the module using
pip3 install module_name. - Use virtual environments to manage dependencies.
Using Virtual Environments for Python Scripts
Virtual environments help you manage Python packages for different projects without conflicts.
How to Create and Use a Virtual Environment
- Install
venvmodule: Usually pre-installed with Python 3. - Create a virtual environment:
python3 -m venv myenv
- Activate the environment:
source myenv/bin/activate
- Install packages inside the environment:
pip install package_name
Run your script while the environment is active.
Deactivate when done:
deactivate
Virtual environments keep your project dependencies clean and isolated.
Automating Python Script Execution
You can automate running Python scripts on Linux using tools like cron jobs.
Setting Up a Cron Job
- Open the crontab editor:
crontab -e
- Add a line to schedule your script. For example, to run every day at 7 AM:
0 7 * * * /usr/bin/python3 /home/user/scripts/script_name.py
- Save and exit.
Cron will run your script automatically at the specified time.
Running Python Scripts with IDEs on Linux
If you prefer a graphical interface, many IDEs support running Python scripts on Linux.
Popular IDEs include:
- VS Code: Lightweight and customizable.
- PyCharm: Full-featured Python IDE.
- Thonny: Beginner-friendly.
These tools let you run, debug, and edit scripts easily.
Summary Table: Ways to Run Python Scripts in Linux
| Method | Command Example | Use Case |
| Run via terminal | python3 script.py | Quick script execution |
| Make executable + shebang | ./script.py after chmod +x | Run script like a program |
| Pass arguments | python3 script.py arg1 arg2 | Scripts needing input parameters |
| Run in background | python3 script.py & | Long-running or detached scripts |
| Use virtual environment | Activate env then run script | Manage dependencies per project |
| Automate with cron | Add cron job with full path | Scheduled script execution |
Conclusion
Running Python scripts in Linux is simple once you know the basics. You can run scripts directly from the terminal, make them executable, pass arguments, or even automate their execution. Using virtual environments helps keep your projects organized and avoids package conflicts.
With these methods, you’ll be able to run your Python scripts efficiently on any Linux system. Whether you’re a beginner or an experienced user, these tips will make your Python coding experience smoother and more productive. So go ahead, open your terminal, and start running your Python scripts on Linux today!
Frequently Asked Questions
How do I check which Python version is installed on Linux?
Open the terminal and type python3 --version or python --version. This shows the installed Python version.
Can I run Python 2 scripts on Linux?
Yes, but Python 2 is outdated. Use python if it points to Python 2, or install Python 2 explicitly. It’s better to update scripts to Python 3.
How do I make a Python script executable?
Add #!/usr/bin/env python3 at the top, then run chmod +x script.py. You can then execute it with ./script.py.
What if my script needs external libraries?
Install them using pip3 install library_name. Use virtual environments to manage dependencies per project.
How can I run a Python script automatically at a specific time?
Use cron jobs by editing crontab with crontab -e and adding a schedule line with the full path to your Python script.
