How to execute local python scripts in Jenkins UI

Create a Jenkins job and run your scripts as shell script from jenkins job. Like this

#!/bin/sh
python <absolute_path_of_python_script>.py

Another way is creating pipeline and execute sh command, which points to your python script. You also can pass parameters via Jenkins UI as dsaydon mentioned in his answer.

sh command can be as follow (is like you run in command line):

sh 'python.exe myscript.py'

Example pipeline step with creating new virtual environment and run script after installing of all requirements

stage('Running python script'){
sh      '''
        echo "executing python script"
        "'''+python_exec_path+'''" -m venv "'''+venv+'''" && "'''+venv+'''\\Scripts\\python.exe" -m pip install --upgrade pip && "'''+venv+'''\\Scripts\\pip" install -r "'''+pathToScript+'''\\requirements.txt" && "'''+venv+'''\\Scripts\\python.exe" "'''+pathToScript+'''\\my_script.py" --path "'''+PathFromJenkinsUI+'''"
        '''
}

where

sh ''' 
   your command here
   ''' 

means multiline shell command (if you really need it)

You also can pass variables from your pipeline (groovy-script) into sh command and, consequently, to your python script as arguments. Use this way '''+argument_value+''' (with three quotes and plus around variable name)

Example: your python script accepts optional argument path and you want to execute it with specific value which you would like to input in your Jenkins UI. Then your shell-command in groovy script should be as follow:

// getting parameter from UI into `pathValue` variable of pipeline script
// and executing shell command with passed `pathValue` variable into it.

pathValue = getProperty('pathValue') 
sh '"\\pathTo\\python.exe" "my\\script.py" --path "'''+pathValue+'''"' 

To execute a Python script under BUILD option- select execute windows batch command - type these cammands.

I am passing the pythonpath because jenkins was not able to access the environmental variables because of access issues.

set PYTHONPATH=%PYTHONPATH%;C:\Users\ksaha029\AppData\Local\Programs\Python\Python3
python C:\Users\ksaha029\Documents\Python_scripts\first.py

instead of handle local script file on each server, you can actually copy all the python script into the "execute shell" under the Build section. it has to start with the relevant python shebang. For example:

#!/usr/bin/env python
your script...

you can also add parameters in the job and use environment variables in your python script. for example

parameter1 = os.environ['parameter1']