我正在使用在Raspberry Pi 3(运行Rasbian)上运行的Jenkins。在此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