我正在使用Jenkins在Raspberry Pi 3上运行(运行Rasbian)。在这个Jenkins实例上,我想使用venv
在虚拟环境中运行Python(3.6)。但是,当我运行我的脚本(使用Jenkins Execute-shell构建步骤)并激活pip
时,它无法正常工作,我无法使用已安装的模块。在终端中运行完全相同的脚本可以很好地工作,我可以使用该模块。
我试图找到这个问题,而且当我通过Jenkins运行脚本时,似乎没有使用pip
中的venv
。
在jenkins中,我在安装并激活venv
之后添加了以下内容以尝试调试它。工作区位于/var/lib/jenkins/workspace/Test Python VENV/
:
>python --version
Python 3.6.5
>pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
>which python
/var/lib/jenkins/workspace/Test Python VENV/venv/bin/python
>which pip
/var/lib/jenkins/workspace/Test Python VENV/venv/bin/pip
>pip install coverage
Collecting coverage
Installing collected packages: coverage
Successfully installed coverage-4.5.3
>pip show coverage | grep Location
Location: /var/lib/jenkins/.local/lib/python2.7/site-packages
>python -m coverage --version
/var/lib/jenkins/workspace/Test Python VENV/venv/bin/python: No module named coverage
但是,如果我在终端(即Jenkins外)运行相同的脚本,它可以工作,我得到以下(目录是~Develop/python_test/
):
>python --version
Python 3.6.5
>pip --version
pip 9.0.3 from /home/pi/Develop/python_test/venv/lib/python3.6/site-packages (python 3.6)
>which python
/home/pi/Develop/python_test/venv/bin/python
>which pip
/home/pi/Develop/python_test/venv/bin/pip
>pip install coverage
Collecting coverage
Installing collected packages: coverage
Successfully installed coverage-4.5.3
>pip show coverage | grep Location
Location: /home/pi/Develop/python_test/venv/lib/python3.6/site-packages
>python -m coverage --version
Coverage.py, version 4.5.3 with C extension
所以我的问题是,当我通过Jenkins运行时,似乎没有使用venv中的Pip,而是使用了系统默认的Pip。由于某种原因,pip --version
和which pip
在詹金斯展示了两条不同的路径。
我正在运行的完整脚本(在Jenkins和终端中)可以在下面找到:
echo Creating and activating virtual environment
python="python3.6"
venv_name="venv"
echo VERSIONS BEFORE VENV
python --version
pip --version
which python
which pip
echo 'Creating venv'
$python -m venv $venv_name
echo 'Activating venv'
. $venv_name/bin/activate
echo VERSIONS AFTER VENV
python --version
pip --version
which python
which pip
pip install coverage
pip show coverage | grep Location
python -m coverage --version
你可以试试Pyenv Jenkins PLugin Pyenv
我测试了你的脚本,它似乎工作,只需添加#!/bin/bash
shebang。