问题:
我有一个执行 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
Jenkins 输出:
[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
Jenkins 输出:
[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 脚本遇到了类似的问题,我最终通过使用解决了它
python xyz.py
而不是
./xyz.py
不幸的是,我无法解释为什么它以这种方式工作,而不是另一种方式,但就我而言,它解决了问题。
关于python - Jenkins 构建期间无法运行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28551122/