无法与jenkins一起运行python脚本

2024-06-11 04:52:06 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我做错什么了?

以下是我尝试过的:

  • 在build部分的configure for the job中,我选择“execute python script”,然后输入testing.py文件——不工作。

  • 我还尝试输入python testing.py和python.exe testing.py—不工作。

  • 试图在“script”编辑文本中编写python脚本-不起作用。

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

更新2:(在尝试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

Tags: inpy脚本wsprocjavawindowtesting
3条回答

在Windows中安装Python时,有一个选项“将Python添加到系统路径”,您应该确保选中了该选项。如果不确定,可以在现有的Python上安全地安装。这对我们詹金斯使用Python插件的人很有用。

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

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

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

相关问题 更多 >