Skip to main content
 首页 » 编程设计

python之无法使用 jenkins 运行 python 脚本

2025年05月04日9softidea

我正在尝试运行 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 作为命令运行时,请提供 Python 可执行文件的完整路径,否则您将不得不在 Jenkins 环境中配置路径。假设您的 python 可执行文件保存在 C:\Python27 文件夹中,然后执行以下命令:

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