【问题标题】:failed to run python scripts with jenkins无法使用 jenkins 运行 python 脚本
【发布时间】:2014-09-16 08:13:55
【问题描述】:

我正在尝试运行 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

【问题讨论】:

    标签: python jenkins


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 请在 Win 命令提示符下键入 where python 的输出。粘贴您从 Win 命令提示符获得的 %PATH% 的输出。此外,在您的 Jenkins 控制台中回显 %PATH%。
      • python 给出的位置:C:\Python27\python.exe 和 %PATH% 在 win 命令行和 jenkins 控制台中是相同的 :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\;
      • 如果您使用的用户与从命令行运行时的用户相同,它应该可以工作。有什么东西不见了。无论如何,您现在可以 做的只是在您在 Jenkins 中指定 python 脚本的部分中输入以下内容:C:\Python27\python.exe file_name。这应该有效。
      • @yehudahs:您尝试过上述建议的更改吗?
      • @Technext 对脚本文件夹应用 jenkins 用户权限对我有用,谢谢。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-23
        • 2016-06-18
        • 2017-12-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多