【问题标题】:Unable to run python script during Jenkins build在 Jenkins 构建期间无法运行 python 脚本
【发布时间】:2015-02-16 22:02:00
【问题描述】:

问题:
我有一个执行 shell 脚本的 Jenkins 构建步骤。该脚本依次调用执行一些加密功能的 python 脚本。但是,当构建执行时,我收到以下错误。

Traceback (most recent call last):
  File "./xyz.py", line 4, in <module>
    import rsa
ImportError: No module named 'rsa' 

Jenkins 的节点有两个版本的 python - 2.7(默认)和 3.4,并为它们安装了rsa。我什至在从站本身上运行了脚本(使用 3.4 版),它运行良好。

到目前为止我做了什么:
我正在使用 EnvInject 插件将PYTHONPATH 指向正确的位置。没有它,我发现PYTHONPATH 是未定义的。

  • 使用 Python 2.7

使用默认版本,我的脚本开头为:#!/usr/bin/env python
詹金斯输出:

[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content 
PYTHONPATH=/usr/local/lib/python2.7

[EnvInject] - Variables injected successfully.
[demo] $ /bin/sh -xe /tmp/hudson9217742060700174209.sh
+ export PYTHONPATH=/jenkins/workspace/demo:/usr/local/lib/python2.7
+ echo /jenkins/workspace/demo:/usr/local/lib/python2.7
/jenkins/workspace/demo:/usr/local/lib/python2.7
+ ./abc.sh
/usr/bin/env: python: No such file or directory  
  • 使用 Python 3.4

本例中的Shebang是#!/usr/bin/env python3
詹金斯输出:

[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content 
PYTHONPATH=/usr/local/lib/python3.4/

[EnvInject] - Variables injected successfully.
[demo] $ /bin/sh -xe /tmp/hudson4592372533933414288.sh
+ export PYTHONPATH=/jenkins/workspace/demo:/usr/local/lib/python3.4/
+ echo /jenkins/workspace/demo:/usr/local/lib/python3.4/
/jenkins/workspace/demo:/usr/local/lib/python3.4/
+ ./abc.sh
Traceback (most recent call last):
  File "./xyz.py", line 4, in <module>
    import rsa
ImportError: No module named 'rsa'  

我什至尝试在脚本本身中执行sys.path.append(os.environ['/usr/local/lib/python3.4/dist-packages/rsa']),但问题仍然存在。

谁能帮我解决这个问题?谢谢。

P.S.- 我对 Python 的了解非常有限。

【问题讨论】:

    标签: python jenkins


    【解决方案1】:

    据我所知,我的 Python 脚本也有类似的问题,我终于通过使用解决了它

    python xyz.py
    

    而不是

    ./xyz.py
    

    不幸的是,我无法解释为什么它以这种方式工作,但不能解释另一种方式,但就我而言,它解决了问题。

    【讨论】:

    • 这对我不起作用。当我尝试时,我得到了./abc.sh: line 27: python: command not found。我什至访问了从节点的脚本控制台并运行了println "which python".execute().text,它正确地打印了/usr/bin/python
    猜你喜欢
    • 2023-01-14
    • 2017-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多