BLOG POSTS
Run Python Script on Ubuntu – Step-by-Step Guide

Run Python Script on Ubuntu – Step-by-Step Guide

Running Python scripts on Ubuntu is a fundamental skill that every developer and system administrator should master, whether you’re deploying applications on production servers, automating system tasks, or developing locally. Ubuntu’s robust environment makes it an ideal platform for Python development, offering multiple Python versions, package managers, and execution methods. This comprehensive guide will walk you through everything from basic script execution to advanced deployment scenarios, including virtual environments, dependency management, troubleshooting common issues, and best practices for different use cases.

Understanding Python on Ubuntu

Ubuntu comes with Python pre-installed, but understanding which version and how to manage multiple installations is crucial for effective development. Most Ubuntu distributions include both Python 2.7 (legacy) and Python 3.x versions, with Python 3 being the default since Ubuntu 20.04.

You can check your current Python installations with these commands:

python3 --version
python --version
which python3
which python

The Python interpreter on Ubuntu follows a specific hierarchy for package management and script execution. System-wide packages are stored in /usr/lib/python3.x/site-packages/, while user-specific packages go to ~/.local/lib/python3.x/site-packages/.

Step-by-Step Implementation Guide

Method 1: Direct Script Execution

The most straightforward way to run a Python script is using the python3 command directly:

# Create a simple test script
echo 'print("Hello from Python on Ubuntu!")' > hello.py

# Run the script
python3 hello.py

Method 2: Making Scripts Executable

For scripts you’ll run frequently, making them executable saves time and makes them feel more like native system commands:

# Add shebang line to your script
echo '#!/usr/bin/env python3
print("This script runs directly!")' > executable_script.py

# Make it executable
chmod +x executable_script.py

# Run it directly
./executable_script.py

Method 3: Using Virtual Environments

Virtual environments are essential for managing dependencies and avoiding conflicts between projects:

# Install venv if not already available
sudo apt update
sudo apt install python3-venv

# Create a virtual environment
python3 -m venv myproject_env

# Activate it
source myproject_env/bin/activate

# Install packages (example)
pip install requests numpy

# Run your script with access to installed packages
python script.py

# Deactivate when done
deactivate

Method 4: System-wide Execution

For scripts that need to run from anywhere on the system:

# Copy script to system PATH
sudo cp myscript.py /usr/local/bin/myscript
sudo chmod +x /usr/local/bin/myscript

# Now you can run it from anywhere
myscript

Managing Dependencies and Package Installation

Python package management on Ubuntu can be handled through multiple approaches, each with specific use cases:

Method Use Case Installation Command Pros Cons
apt (system packages) System-level dependencies sudo apt install python3-requests Stable, integrated with system Limited packages, older versions
pip (global) Quick testing, single-user systems pip3 install requests Latest versions, extensive library Can break system packages
pip (user) Personal development pip3 install –user requests Safe, no sudo required User-specific only
Virtual environments Project isolation pip install requests (in venv) Complete isolation, reproducible Extra setup overhead

Real-World Examples and Use Cases

Web Scraping Script with Dependencies

# setup_scraper.sh
#!/bin/bash
python3 -m venv scraper_env
source scraper_env/bin/activate
pip install requests beautifulsoup4 lxml
echo "Environment ready for web scraping"
# scraper.py
#!/usr/bin/env python3
import requests
from bs4 import BeautifulSoup
import sys

def scrape_website(url):
    try:
        response = requests.get(url)
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        return soup.get_text()[:200] + "..."
    except requests.RequestException as e:
        print(f"Error scraping {url}: {e}")
        return None

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python3 scraper.py ")
        sys.exit(1)
    
    result = scrape_website(sys.argv[1])
    if result:
        print(result)

System Monitoring Script

# monitor.py
#!/usr/bin/env python3
import psutil
import time
import json
from datetime import datetime

def get_system_stats():
    return {
        'timestamp': datetime.now().isoformat(),
        'cpu_percent': psutil.cpu_percent(interval=1),
        'memory_percent': psutil.virtual_memory().percent,
        'disk_usage': psutil.disk_usage('/').percent,
        'load_average': psutil.getloadavg()
    }

def main():
    while True:
        stats = get_system_stats()
        print(json.dumps(stats, indent=2))
        time.sleep(60)

if __name__ == "__main__":
    main()

Automated Deployment Script

# deploy.py
#!/usr/bin/env python3
import subprocess
import sys
import os

def run_command(cmd):
    result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
    if result.returncode != 0:
        print(f"Error running command: {cmd}")
        print(f"Error: {result.stderr}")
        sys.exit(1)
    return result.stdout

def deploy_application():
    print("Starting deployment...")
    
    # Update system packages
    run_command("sudo apt update")
    
    # Install required system packages
    run_command("sudo apt install -y python3-pip nginx")
    
    # Setup application environment
    run_command("python3 -m venv /opt/myapp/venv")
    run_command("/opt/myapp/venv/bin/pip install -r requirements.txt")
    
    # Start services
    run_command("sudo systemctl enable nginx")
    run_command("sudo systemctl start nginx")
    
    print("Deployment completed successfully!")

if __name__ == "__main__":
    deploy_application()

Common Issues and Troubleshooting

Permission Errors

One of the most frequent issues when running Python scripts on Ubuntu involves file permissions:

# Fix permission denied errors
chmod +x script.py

# If script needs to write files
mkdir -p /tmp/python_output
python3 script.py

# For system-level operations
sudo python3 system_script.py

Module Import Errors

When Python can’t find modules, these troubleshooting steps usually resolve the issue:

# Check Python path
python3 -c "import sys; print('\n'.join(sys.path))"

# Add custom module path
export PYTHONPATH="/path/to/your/modules:$PYTHONPATH"
python3 script.py

# Or modify path within script
import sys
sys.path.append('/path/to/your/modules')
import your_module

Version Compatibility Issues

Managing multiple Python versions can cause conflicts:

# Install specific Python version
sudo apt install python3.9 python3.9-venv python3.9-pip

# Use specific version
python3.9 script.py

# Create virtual environment with specific version
python3.9 -m venv specific_env
source specific_env/bin/activate

Environment Variables and Configuration

Many Python applications require specific environment variables:

# Set environment variables for single run
DATABASE_URL="sqlite:///test.db" API_KEY="your_key" python3 app.py

# Or create environment file
echo "DATABASE_URL=sqlite:///test.db
API_KEY=your_key
DEBUG=True" > .env

# Load in Python script
import os
from dotenv import load_dotenv

load_dotenv()
database_url = os.getenv('DATABASE_URL')

Performance Considerations and Optimization

Running Python scripts efficiently on Ubuntu involves several optimization strategies:

  • Use PyPy for CPU-intensive tasks: PyPy can provide 2-10x performance improvements for certain workloads
  • Profile your code: Use cProfile to identify bottlenecks before optimization
  • Leverage multiprocessing: Ubuntu’s SMP capabilities can significantly speed up parallel workloads
  • Optimize I/O operations: Use asynchronous libraries like asyncio for I/O bound tasks
# Performance profiling example
python3 -m cProfile -o profile_output.prof script.py

# Analyze results
python3 -c "import pstats; p=pstats.Stats('profile_output.prof'); p.sort_stats('time').print_stats(10)"

Automation and Scheduling

Ubuntu provides several mechanisms for automating Python script execution:

Cron Jobs

# Edit crontab
crontab -e

# Add entries for scheduled execution
# Run every day at 2 AM
0 2 * * * /usr/bin/python3 /home/user/scripts/daily_backup.py

# Run every 5 minutes
*/5 * * * * /home/user/venv/bin/python /home/user/scripts/monitor.py

Systemd Services

# Create service file: /etc/systemd/system/myapp.service
[Unit]
Description=My Python Application
After=network.target

[Service]
Type=simple
User=myuser
WorkingDirectory=/home/myuser/myapp
Environment=PYTHONPATH=/home/myuser/myapp
ExecStart=/home/myuser/myapp/venv/bin/python app.py
Restart=always

[Install]
WantedBy=multi-user.target
# Enable and start service
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service

# Check status
sudo systemctl status myapp.service

Security Best Practices

Security considerations are crucial when running Python scripts on production Ubuntu systems:

  • Never run scripts as root unless absolutely necessary: Create dedicated service users for applications
  • Validate input: Always sanitize user input and command-line arguments
  • Use virtual environments: Isolate dependencies to prevent system compromise
  • Keep packages updated: Regularly update both system packages and Python libraries
  • Implement proper logging: Monitor script execution and access patterns
# Create dedicated user for script execution
sudo useradd -r -s /bin/false scriptuser
sudo mkdir -p /opt/scripts
sudo chown scriptuser:scriptuser /opt/scripts

# Run script as specific user
sudo -u scriptuser python3 /opt/scripts/secure_script.py

Integration with Development Workflows

Modern Python development on Ubuntu often involves integration with various tools and platforms. Whether you’re deploying to VPS services or managing dedicated servers, having a robust script execution strategy is essential.

Docker Integration

# Dockerfile for Python application
FROM ubuntu:22.04

RUN apt-get update && apt-get install -y python3 python3-pip
WORKDIR /app
COPY requirements.txt .
RUN pip3 install -r requirements.txt
COPY . .

CMD ["python3", "app.py"]

Git Hooks for Automated Testing

# .git/hooks/pre-commit
#!/bin/bash
python3 -m pytest tests/
python3 -m flake8 src/
echo "All tests passed!"

For comprehensive documentation on Python deployment and execution, refer to the official Python documentation for Unix platforms and the Ubuntu Community Python documentation.

Understanding these various approaches to running Python scripts on Ubuntu will significantly improve your development workflow and deployment capabilities. Whether you’re automating system tasks, developing web applications, or managing data processing pipelines, mastering these techniques ensures reliable and efficient script execution across different environments and use cases.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked