Skip to main content
New Participant
October 31, 2019
Answered

Question about executing python files with node.js "child_process"

  • October 31, 2019
  • 1 reply
  • 1133 views

Hello!
I am trying to execute a python file in my PP extention, i read that 'node.js' and 'child_process' is the way to go if you want to run CLI commands in javascript, so i went to my XML file and enabled it ( it works as it should with other methods like "fs"). Also i have a folder inside my extention with all the necessery modules that i want to use. I tried to execute the same piece of code as a seperate .js file and it works fine. In my extention panel  a button is attached to call the function but nothing happens, it completely ignores it.

These are the functions i am trying to use:

 

<script src="./lib/CSInterface.js"></script>
		<script src="./lib/jquery-1.9.1.js"></script>
		<script type="text/javascript">

			function execute (command) {			
				const  exec  = require('child_process').exec
				exec(command, (err, stdout, stderr) => {
					process.stdout.write(stdout)
				})
			}
			function runPython() {
				execute('python pythonFile.py')
			}
		</script>

 

The runPython function is attached to a run-button in the index.html file that should execute the scirpt. I tried to use the "child_process" method with many differend compinations but i can't figure out what might be the probelm here. Any ideas on the subject? Has anyone expirienced something similar?
Thanks in advantage.

This topic has been closed for replies.
Correct answer kuaiyl353582254d18
window.cep.process.createProcess("C:\\Windows\\explorer.exe", mogrtParentPath);

I also want to use this method, but what should I do if I report the error "window is undefined"

1 reply

Bruce Bullis
Community Manager
Community Manager
November 1, 2019
Here's a JavaScript function that builds and executes a Python command line, from within a panel. 


function processMoGRT (){
    var csInterface = new CSInterface();
    var OSVersion   = csInterface.getOSInformation();
    var filetypes = new Array();
    filetypes[0] = "mogrt";
    var result = window.cep.fs.showOpenDialog(false,false,"Select a .mogrt file", "~/Desktop",filetypes);
 
    var mogrtPath = result.data[0];


    var extPath    = csInterface.getSystemPath(SystemPath.EXTENSION);
    
    var sep        = "/";

    if (OSVersion.indexOf("Windows") > 0){
        sep = "\\\\";    
    }

    var mogrtParentPath = mogrtPath.substring(0, mogrtPath.lastIndexOf(sep));

    var pyPath = extPath + sep + 'Foiltop.py';

    var processResult = window.cep.process.createProcess("usr/bin/python", pyPath, mogrtPath);
    
    if (OSVersion.indexOf("Windows") >=0){
        window.cep.process.createProcess("C:\\Windows\\explorer.exe", mogrtParentPath);
    } else {
        window.cep.process.createProcess("/usr/bin/open", mogrtParentPath);
    }
    var resultFileName = mogrtParentPath + sep + "Foiltop_results.txt";
    
    var resultFileContents = window.cep.fs.readFile(resultFileName);//, cep.encoding.Base64);
    
    if (0 === result.err) {
        alert(resultFileContents.data);
    } else {
        alert("Failed to read results file.");
    }
kuaiyl353582254d18Correct answer
New Participant
February 21, 2024
window.cep.process.createProcess("C:\\Windows\\explorer.exe", mogrtParentPath);

I also want to use this method, but what should I do if I report the error "window is undefined"