Jenkins job to create python virtual environment

Jenkins job to create python virtual env in 2023

Jenkins is one of the most popular open-source CI tools, and most companies use it to automate their production workflow. In this blog, we will learn how to use the Jenkins job to create python virtual env.

What is a python virtual environment?

A python virtual environment is an isolated python environment that allows you to work on a specific python version without worrying about the other projects.

Suppose you want to work on python version 3.7 for a specific version of the project, and you have python version 3.1 installed in your system.

The simple solution is to install python version 3.7 also in your system. But this solution is not the optimal way of installing multiple python versions. The other way is to have a separate virtual environment and install the required python version in that.

In the next session, I will explain how to set up a python virtual environment.

How to setup a python virtual environment

Let’s create a folder named my_virtual_env

mkdir ~/my_virtual_env

Type the below command to create a virtual environment for python3

python3 -m venv ~/my_virtual_env

Now type the below command to activate the virtual environment

source ~/my_virtual_env/bin/activate

Run the below command to check the python package

which python

To deactivate the python version, run the below command


Now let’s implement the same scenario using the Jenkins job

Jenkins python virtual environment example

Goto Jenkins UI and click on a new item.

select the freestyle project and provide the item name

Goto build and click on Execute shell
now type the below command in execute shells

save the below job and build the project now.

Jenkins python virtualenv plugin

A Jenkins plugging called Pyenv pipeline helps you create the Python virtual environment in your Jenkins pipeline. The syntax for defining the python virtual environment follows the below pattern.

withPythonEnv('python') {
    // Uses the default system installation of Python
    // Equivalent to withPythonEnv('/usr/bin/python') 

If you wish to install any specific version of python, then you can achieve this by specifying the python version inside the withPythonEnv block.

withPythonEnv('/usr/bin/python3.2') {
    // Uses the specific python3.2 executable located in /usr/bin

More details about the usage of theJenkins python virtualenv plugin can be found here

Python-Jenkins example

In this session, you will learn how to interact with Jenkins using python. Python provides a library called python-Jenkins which allows users to manage the Jenkins server through the Jenkins REST endpoints.

To install thepython-Jenkins, type the below command

pip install python-jenkins

Now connect to the python shell and type the below command to the Jenkins server

import jenkins
server = jenkins.Jenkins('http://localhost:8080', username='admin', password='admin')
Python-Jenkins library

You have to pass the Jenkins username and password in the above code. In my case, the username and password are admin/admin.

Type the below command to get the Jenkins version


Similarly, to get the count of all the jobs from Jenkins by typing the below command.


Get the jobs list by the get_jobs() command.

[{'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'function', 'url': 'http://localhost:8080/job/function/', 'color': 'blue', 'fullname': 'function'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'Jenkins declarative Pipeline example-github', 'url': 'http://localhost:8080/job/Jenkins%20declarative%20Pipeline%20example-github/', 'color': 'blue', 'fullname': 'Jenkins declarative Pipeline example-github'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'Jenkins pipeline define variable', 'url': 'http://localhost:8080/job/Jenkins%20pipeline%20define%20variable/', 'color': 'blue', 'fullname': 'Jenkins pipeline define variable'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'Jenkins pipeline example-Hello World', 'url': 'http://localhost:8080/job/Jenkins%20pipeline%20example-Hello%20World/', 'color': 'blue', 'fullname': 'Jenkins pipeline example-Hello World'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'Jenkins Pipeline example-multiple stages', 'url': 'http://localhost:8080/job/Jenkins%20Pipeline%20example-multiple%20stages/', 'color': 'blue', 'fullname': 'Jenkins Pipeline example-multiple stages'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'Jenkins pipeline-if statement', 'url': 'http://localhost:8080/job/Jenkins%20pipeline-if%20statement/', 'color': 'blue', 'fullname': 'Jenkins pipeline-if statement'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'scripted pipeline-when', 'url': 'http://localhost:8080/job/scripted%20pipeline-when/', 'color': 'blue', 'fullname': 'scripted pipeline-when'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'scripted_Pipeline_ex_1', 'url': 'http://localhost:8080/job/scripted_Pipeline_ex_1/', 'color': 'blue', 'fullname': 'scripted_Pipeline_ex_1'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'scripted_Pipeline_ex_2', 'url': 'http://localhost:8080/job/scripted_Pipeline_ex_2/', 'color': 'red', 'fullname': 'scripted_Pipeline_ex_2'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'scripted_Pipeline_ex_4', 'url': 'http://localhost:8080/job/scripted_Pipeline_ex_4/', 'color': 'blue', 'fullname': 'scripted_Pipeline_ex_4'}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'name': 'test', 'url': 'http://localhost:8080/job/test/', 'color': 'notbuilt', 'fullname': 'test'}]

Similarly, there are many methods and functions available that will help you interact with Jenkins using python. More details about the Python-Jenkins library can be found here.


I hope you like this tutorial. In this blog, we have learned about the python virtual environment and how to create a python virtual environment in Jenkins using different methods. Finally, we have learned how to interact with Jenkins using the Python Jenkins library.

Please let me know if you are facing any issues while following along.

More to Read?

Jenkins operator

Jenkins Workflow

Jenkins scripted pipeline

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top