top of page
  • Writer's picturePierrick Kinif

How to Install a Python Virtual Environment on Jenkins

A virtual environment is an isolated Python environment that allows you to work on multiple Python projects with different versions of packages or Python itself without interfering with each other. This post will show you how to install a Python virtual environment on Jenkins.


Step 1: Installing Virtualenv


Virtualenv is a tool used in Python programming that creates isolated Python environments, each with its own dependencies, packages, and versions of Python. This means you can create multiple isolated Python environments on the same machine, each with different versions of packages or Python itself, without interfering with each other.

To install Virtualenv, you first need to install pip, the Python package manager. You can do this by running:

sudo apt-get install python-pip

Once pip is installed, you can install Virtualenv by running:

sudo pip install virtualenv


Step 2: Creating a New Virtual Environment


Choose a directory where you want to create your new environment. This can be anywhere on your file system.

sudo mkir /python-env
sudo virtualenv /python-env/python-env-project-1

This will create a new virtual environment in the "/python-env" directory.

You can now create a text requirement file with your packages requirement.


cd python-env-project-1
sudo nano requirement.txt

Here is an example - you can copy/paste those values in the script


pandas==1.4.4
numpy==1.22.4
pandas_ta==0.3.14b
scikit-learn==1.2.2
yfinance==0.1.74
scipy==1.10.1
pyodbc==4.0.35
sqlalchemy==1.4.47
psycopg2==2.9.5
openpyxl==3.1.2
statsmodels==0.13.5

Then install the dependencies into the environment:


pip install -r requirement.txt

You can create as many Python environments as you need by repeating these steps with different directory names. When you activate an environment, you will be using the Python interpreter and packages installed in that environment, and any changes you make to the environment will be isolated from your system Python installation and other Python environments.



Step 3: Using the Virtual Environment in a Jenkins Job


To use the virtual environment in a Jenkins job, you need to activate it in the shell script before running any Python commands. For example:


source /python-env/python-env-project-1/bin/activate
python3 your_script.py

It's a good practice to link your Jenkins job to a GitHub repository where you store your Python script. This way, you can easily manage and run your code in a virtual environment.


Conclusion


Installing a Python virtual environment on Jenkins is a useful technique to work on multiple Python projects without interference. By following the steps outlined in this post, you can create isolated Python environments, each with its own dependencies, packages, and versions of Python. This will allow you to manage your code easily and run it in a virtual environment. It's a best practice to link your Jenkins job to a GitHub repository, which can help you manage your code and track changes effectively. Overall, using a virtual environment in Jenkins can enhance your productivity and help you work on Python projects seamlessly.







108 views0 comments

Recent Posts

See All
bottom of page