'how can i exec a python-file WITH imports in PHP?

Im very new to python and i was not able to find 100% fitting answer here :-/

I made a python-script with a list of imports (os, pyplot, pandas, tensorflow ..) now i would like to run this script on a webserver and exec it with PHP.

$command = escapeshellcmd('python scripts/XXX.py');
$output = shell_exec($command);
echo $output;

so far no problem for a simple hello world. but how i can handle the imports? Im using Visual Studio code, i dont find a export function to export with all libraries, i just can do a default "save as", i also trieds to save as .bat

whats the best way to get a python file executeable with PHP and those list of imports? i just wanna pass 3 vars and get the output

Best Regards



Solution 1:[1]

Hope it helps as a reference.

<?php
$command = escapeshellcmd('python test.py arg1 arg2 arg3');
$output = shell_exec($command);
echo $output;
?>

Solution 2:[2]

If you don't mind a script, here is the basic algorithm:

function clean_ranges(txt) {
  txt = txt.replace(/:00/g, '');

  var ranges = txt.split(' • ');

  for (var i=0; i<ranges.length; i++) { 
    if (ranges[i].split('am').length>2) ranges[i] = ranges[i].replace('am','');
    if (ranges[i].split('pm').length>2) ranges[i] = ranges[i].replace('pm','');
  } 

  return ranges.join(' • ');
}

var str = '• 9:15am-10:00am • 1:00pm-3:30pm • 11:30am-12:30pm • 9:00am-1:00pm';

console.log(clean_ranges(str));

The full implementation for InDesign is here:

function clean_ranges(txt) {
    txt = txt.replace(/:00/g, '');

    var ranges = txt.split(' • ');

    for (var i = 0; i < ranges.length; i++) {
        if (ranges[i].split('am').length > 2) ranges[i] = ranges[i].replace('am', '');
        if (ranges[i].split('pm').length > 2) ranges[i] = ranges[i].replace('pm', '');
    }

    return ranges.join(' • ');
}

try {
    var txt = app.selection[0].contents;
    if (txt.split('-').length < 2) exit(); // do nothing if selected text has no '-'
    app.selection[0].contents = clean_ranges(txt);
} catch(e) {}

It will replace a selected text.

Solution 3:[3]

This can be done with GREP, but you will need 2 searches:

am(?=-\d{1,2}:\d{2}am)

pm(?=-\d{1,2}:\d{2}pm)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 ym y
Solution 2 RobC
Solution 3 cybernetic.nomad