无法使用jenkins运行python脚本

时间:2014-09-16 08:13:55

标签: python jenkins

我正在尝试运行Python作业。我创建了以下文件夹:

  

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

并将脚本“testing.py”添加到其中。

脚本非常简单:

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

但是我从Jenkins运行时遇到以下错误(如果我从命令行运行它可以工作):

>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)

我做错了什么?

以下是我的尝试:

  • 在构建部分的作业配置中我选择“执行python脚本”而不是输入testing.py文件 - 无法正常工作。

  • 我也尝试进入那里python testing.py和python.exe testing.py - 无法正常工作。

  • 尝试在“脚本”编辑文本中编写python脚本 - 无法正常工作。

  • 如果我将执行类型从python更改为批处理文件,而不是显示它通过但实际上它没有运行python脚本。

update2:(在尝试Technext解决方案后)我收到了下一个错误:

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

3 个答案:

答案 0 :(得分:0)

由于python脚本在命令行上运行良好但在运行Jenkins时出现问题,因此很可能意味着运行Jenkins的 user 在查找Python可执行文件时遇到问题,即python.exe 。如果您可以(并且可行)更改Jenkins用户,请使用我描述的here过程进行更改。 使其在命令提示符下成功运行程序的用户

答案 1 :(得分:0)

每当在批处理文件中将python作为命令运行时,请提供Python可执行文件的完整路径,否则您将必须在Jenkins环境中配置路径。假设你的python可执行文件保存在C:\Python27文件夹中,然后执行以下命令:

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

答案 2 :(得分:0)

在Windows中安装Python时,可以选择“将Python添加到系统路径”,您应确保选中该选项。如果您不确定,可以安全地安装现有的Python。这在Jenkins中使用Python插件为我们工作。