yehudahs
yehudahs

Reputation: 2668

failed to run python scripts with jenkins

I am trying to run a Python job. I have created the following folder:

C:\Users\herod\jenkins_ws\workspace\window_testing

and added the script "testing.py" to it.

The script is very simple:

if __name__ == "__main__":
    print "hellow world !"
    f = open('test_log.txt','w')
    f.write("hello\n")
    f.close

But I am getting the following error when running it from Jenkins (if I run it from command line it works):

>Building remotely on windows1 (widndows_genereal) in workspace C:\Users\herod\jenkins_ws\workspace\window_testing
[window_testing] $ python C:\windows\TEMP\hudson5234791200924972506.py
    The system cannot find the file specified
    FATAL: command execution failed
    java.io.IOException: Cannot run program "python" (in directory "C:\Users\herod\jenkins_ws\workspace\window_testing"): CreateProcess error=2, The system cannot find the file specified
        at java.lang.ProcessBuilder.start(Unknown Source)
        at hudson.Proc$LocalProc.<init>(Proc.java:244)
        at hudson.Proc$LocalProc.<init>(Proc.java:216)
        at hudson.Launcher$LocalLauncher.launch(Launcher.java:775)
        at hudson.Launcher$ProcStarter.start(Launcher.java:355)
        at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:1024)
        at hudson.Launcher$RemoteLaunchCallable.call(Launcher.java:991)

What am I doing wrong ?

Here is what I have tried:

update2: (after trying Technext solution) I got the next error:

Building remotely on windows1 (widndows_genereal) in workspace C:\Users\herod\jenkins_ws\workspace\window_testing
[window_testing] $ python C:\Users\herod\AppData\Local\Temp\hudson4767788636447260218.py
Traceback (most recent call last):
  File "C:\Users\herod\AppData\Local\Temp\hudson4767788636447260218.py", line 1, in <module>
    testing.py
NameError: name 'testing' is not defined
Build step 'Execute Python script' marked build as failure
Finished: FAILURE

Upvotes: 2

Views: 25674

Answers (3)

aaron
aaron

Reputation: 168

When you install Python in Windows, there is an option to "Add Python to System Path" which you should make sure is selected. You can safely install over your existing Python if you're not sure. This worked for us in Jenkins using the Python plugin.

Upvotes: 1

Akhil
Akhil

Reputation: 479

Whenever running python as a command in batch file, Give the full path of Python executable or you will have to configure the path in the Jenkins environment. Say your python executable is kept in C:\Python27 folder, then execute the following:

C:\Python27\python.exe <full path of python file to execute>

Upvotes: 2

Technext
Technext

Reputation: 8117

Since the python script runs fine on the command line but has issues when running through Jenkins, it most probably means that the user with which Jenkins is running has issues finding Python executable i.e., python.exe. If it is possible (and feasible) for you to change the Jenkins user, then please change it using the process i described here. Make it run as the same user with which you are running the program successfully on command prompt.

Upvotes: 1

Related Questions