ModuleNotFoundError when activating venv in CGI script

Hi everybody,

I’m new to using the transformers library and have been experimenting with it via pytorch and venv.

If I activate the previously installed virtual environment from a shell, it works just fine.
However, not if I activate it in a python CGI script, like so:

import subprocess

# Defining function for activating/deactivating virtual environment
def activate_virtual_environment():

    # Build the path to the activation script
    activation_script = f"/xampp/htdocs/cgi_bin/env/Scripts/activate"

    # Activate the virtual environment using subprocess
    subprocess.run([activation_script], shell=True)

def deactivate_virtual_environment():
    # Deactivate the virtual environment using subprocess
    subprocess.run(["deactivate"], shell=True)

# Activating virtual environment
activate_virtual_environment()

# Importing 'pipeline' 
from transformers import pipeline

If i activate the virtual environment like this, I get a “ModuleNotFoundError: No module named ‘transformers’”, when the script is executed.

The path to the virtual environment is definitely correct, what am I overlooking?
Thanks in advance for your guidance :slight_smile: