Cleared
Cleared

Reputation: 2590

Pip not activated properly when using venv in Jenkins

I am using Jenkins running on a Raspberry Pi 3 (running Rasbian). On this Jenkins instance, I want to run Python (3.6) in a virtual env, using venv. However when i run my script (using Jenkins Execute-shell build-step) and activate pip, it does not work properly and I am not able to use the installed modules. Running the exact same script in the terminal works perfectly, and I can use the module.

I have tried to find the issue, and it seems like the pip in the venv is not used when I run the script through Jenkins.

In jenkins I have added the following after the venv has been installed and activated to try to debug it. Workspace is located in /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

However if I run the same script in a terminal (i.e. outside Jenkins) it works and i get the following (directory is ~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

So my problem is that when I run this through Jenkins, the Pip in the venv is does not seem to be used, instead the systems default Pip is used. And for some reason pip --version and which pip shows two different paths in Jenkins.

The full script that I am running (both in Jenkins and in the terminal) can be found below:

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 

Upvotes: 0

Views: 2057

Answers (1)

dejanualex
dejanualex

Reputation: 4328

You could try Pyenv Jenkins PLugin Pyenv

I tested your script and it seems to work, just add #!/bin/bash shebang.

Upvotes: 1

Related Questions