How to Run a Python File in Jenkins

Running Python files in Jenkins is essential for automating scripts, executing tests, and integrating Python applications into CI/CD pipelines. In this guide, you’ll learn how to run a Python file in Jenkins, set up Python in Jenkins jobs, and troubleshoot common issues.

Why Use Jenkins to Run a Python File?

Jenkins allows developers to automate Python script execution efficiently. Whether you’re managing data pipelines, testing software, or deploying machine learning models, running a Python file in Jenkins streamlines the process.

Key Benefits:

  • Continuous Integration (CI) – Automate Python tests before deployment.
  • Task Scheduling – Run Python scripts at specific intervals.
  • Machine Learning Pipelines – Automate model training and evaluation.
  • Automated Reports – Generate and email reports using Python.

How to Run a Python File in Jenkins (Step-by-Step Guide)

1. Running a Python File in a Jenkins Freestyle Job

  1. Open Jenkins and create a New Item → Select Freestyle Project.
  2. In the Build section, click Add Build StepExecute Shell (Linux/macOS) or Execute Windows Batch Command (Windows).
  3. Add the command to run your Python file:
    • Linux/macOS: python3 /path/to/script.py
    • Windows: python C:\path\to\script.py
  4. Click Save, then Build Now to execute the Python script in Jenkins.

2. Running a Python File in a Jenkins Pipeline

For advanced users, running a Python file in Jenkins via pipelines is a better approach.

Jenkins Pipeline Script for Python Execution

pipeline {
    agent any
    stages {
        stage('Run Python File') {
            steps {
                sh 'python3 /path/to/script.py'  // Use 'python' on Windows
            }
        }
    }
}

3. Running a Python File in Jenkins with Virtual Environments

To prevent dependency issues, use a virtual environment:

pipeline {
    agent any
    stages {
        stage('Setup and Run') {
            steps {
                sh '''
                python3 -m venv venv
                source venv/bin/activate
                pip install -r requirements.txt
                python script.py
                deactivate
                '''
            }
        }
    }
}

On Windows, replace source venv/bin/activate with venv\Scripts\activate.

Using Environment Variables in Jenkins for Python

To securely handle sensitive data like API keys:

  1. Go to Build EnvironmentInject environment variables.
  2. Add variables like: API_KEY=your_secret_api_key
  3. Modify Python to read the variable: import os api_key = os.getenv("API_KEY") print(f"API Key: {api_key}")

Advanced: Running a Python File in Jenkins with Docker

For consistent environments, run Python inside a Docker container:

pipeline {
    agent {
        docker { image 'python:3.10' }
    }
    stages {
        stage('Run Python File') {
            steps {
                sh 'python /path/to/script.py'
            }
        }
    }
}

Troubleshooting: Fixing Python Issues in Jenkins

  • Python command not found? Add Python to the system PATH.
  • Permission denied? Grant execution permission: chmod +x script.py
  • Missing dependencies? Install required packages: pip install -r requirements.txt

Final Thoughts

Now you know how to run a Python file in Jenkins, whether using Freestyle jobs, Pipelines, virtual environments, or Docker. Jenkins simplifies Python automation, making it a must-have tool for software developers, data scientists, and DevOps professionals.

Would you like to see how running Python files in Jenkins can be optimized for large-scale projects? Let us know in the comments!

How to Create an Online Python Compiler Using Docker: Step-by-Step Guide

1 thought on “How to Run a Python File in Jenkins”

  1. Pingback: How to File Copy in Python -

Leave a Comment

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

Scroll to Top