'Activating python venv in laravel

I want to activate a virtual environment in my laravel controller with Symfony Process. My python venv and scripts are located in the public storage folder.

I am able to run simple python scripts with the following code:

    $command =  ['Python3', 'storage/STT/index.py'];

    // start new process
    $process = new Process($command);

    // run process
    $process->run();


    // check if process went sucessfull after is is finished
    // executes after the command finishes
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }

    return response($process->getOutput(), 200);

I catch the response in a Vue front end. The python script prints hello world. The process of returning the process output works fine.

Now I want to run a different script called main.py that contains a few imports. I need to activate a virtual environment whenever I want to run that script. I changed the $command to:

    $command =  ['source', 'storage/STT/venv/bin/activate'];

    

The error that it gives:

message: "The command "'source' 'storage/STT/venv/bin/activate'" failed.\n\nExit Code: 127(Command not found)\n\nWorking directory: /Users/user/Documents/Audioanalyse_api/public\n\nOutput:\n================\n\n\nError Output:\n================\nsh: line 0: exec: source: not found\n"

Is there any solution to the error or is starting a venv via Process not possible?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source