2

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:

  • in the configure for the job at the build section I choose "execute python script" and than entered the testing.py file - not working.

  • I also tried to enter there python testing.py and python.exe testing.py - not working.

  • Trying to write a python script in the "script" edit text - not working.

  • If I change the execute type from python to batch file and than it shows that it pass but actually it didn't run the python script.

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
yehudahs
  • 2,488
  • 8
  • 34
  • 54

3 Answers3

2

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>
Akhil
  • 479
  • 3
  • 13
1

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.

Community
  • 1
  • 1
Technext
  • 7,887
  • 9
  • 48
  • 76
  • Please paste the output of `where python` when you type it on Win command prompt. Paste the output of %PATH% that you get from Win command prompt. Also, echo %PATH% in your Jenkins console. – Technext Sep 16 '14 at 09:24
  • where python gave : C:\Python27\python.exe and %PATH% was the same in win command line and in jenkins console :C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Debugging Tools for Windows (x64);c:\UnixTools;C:\Program Files (x86)\CMake 2.8\bin;C:\Program Files (x86)\Git\cmd;C:\Program Files (x86)\Git\bin;C:\Python27\Scripts;C:\Python27\; – yehudahs Sep 16 '14 at 10:12
  • 1
    It should work if you're using the same user as the one that was there when running from command line. There's something that's missing. Anyways, what you _can_ do now is simply enter the following in the section where you've specified your python script in Jenkins: `C:\Python27\python.exe file_name`. This should work. – Technext Sep 16 '14 at 10:17
  • @yehudahs: Did you try the suggested changes mentioned above? – Technext Sep 17 '14 at 07:18
  • @Technext applying jenkins user permission on scripts folder works with me, thank you. – Yuri Nov 08 '17 at 13:50
1

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.

aaron
  • 168
  • 1
  • 5