Skip to content

Configuring a Virtual Environment for Python in eLegere

Warning

Configuring a virtual environment for Python scripts requires access to the eLegere machine. If you don't own the privileges to access the eLegere machine, ask your administrator to perform the steps in this document for you.

A virtual environment for Python is an isolated compartment where you can have your Python configuration separated from that of the system.

Learn how to create a virtual environment for Python for your eLegere installation. Creating a dedicated environment helps you to setup different packages sets for each Domani or specific users.

This tutorial explains:

  1. The Python's virtual environment configuration's process in Windows.
  2. How to launch the virtual environment's Python scripts through Custom Actions.

Note

The tutorial employs the virtualenv Python package in place of the standard venv module.

Install through pip the virtualenv package before starting.

After having installed virtualenv, you will find the executable virtualenv.exe under the Scripts folder in the machine's Python installation folder.

Configuring the Python's Virtual Environment

Follow the steps below to configure the Python's environment on the eLegere machine through virtualenv.

The installation assumes the Command Line Interface (CLI) use in Windows.

  1. Identify the machine's folder where you want to setup the Python's virtual environment. Move to such folder in the CLI. (folder-environment stands for the directory where you want to create the virtual environment.)

    1
    cd folder-environment
    
  2. Launch the following command in the folder chosen at the previous step. (Replace the path in the example with path where you want to create the Python's virtual environment.)

    1
    python -m virtualenv  C:\[Insert the path where you want to install the virtual environment]
    

    The command above creates a virtual environment using the default Python version on the machine.

    Tip

    If you have multiple Python versions and you want to point a certain one, use the following command instead.

    1
    virtualenv --python "C:\[Insert here the path to the Python's version]\python.exe" venv
    

    Wait then for virtualenv to complete the operation.

  3. Launch the following command in the virtual environment's folder to activate it.

    1
    .\venv\Scripts\activate
    

    Tip

    Check if you have activated the environment correctly by typing which python3 in the CLI. If the configuration succeeded, the command displays the virtual environment's path.

  4. Install all the packages you require through the python -m pip install [package name] command. (Check the pip documentation for more information.)

  5. Shut down the CLI, no further actions required.

Tip

At any time, type in the CLI deactivate to turn off the environment.

Success

The Python virtual environment's configuration is completed. Check the next section to learn how to launch a Python script inside an environment from a Custom Action.

Launching a Python Script in the Virtual Environment through Custom Actions

Learn how to launch a Python script within an environment from a Python Script Custom Action.

Follow the instructions in Run Python Scripts inside Your Application to configure the Python Script Custom Action as usual.

When you insert the Script Path in the Parameters: supply the Python script below instead of pointing to the desired script.

Why the Intermediate Python Script?

The script below opens a process from within the virtual environment, executes the desired Python script, and reports the result.

By default, Custom Actions run the scripts by using the eLegere installation's Python configuration on the machine. You must use a intermediate Python script executing code within the virtual environment.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# The operation requires the subprocess module.

import subprocess

# Popen method launches a new CLI process to execute commands. The desired Python script and its environment run in such process. 
# Replace 'environment-folder' in the string with the path to the configured environment.

p = subprocess.Popen(r"C:\environment-folder\Scripts\python.exe C:\folder\test.py", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding="utf-8")

# A FOR IN loop catches all the output messages from the process through the readlines method. The loop prints them in this script's output. 

for line in p.stdout.readlines():
    print(line)

Repeat the operation for each Python Script you want to execute from the virtual environment.

Success

You have configured a Python Script Custom Action to run a script from a virtual environment previously set up.