Home > Software design >  execute a python project from a node js server
execute a python project from a node js server

Time:01-26

I wrote a rather complex algorithm in Python that uses some special libraries that only Python has. Now I am trying to write a server in node js that can get Api requests that run my algorithm. So I copied the project of the algorithm (which runs fine via Pycharm) to the node's project folder and now it returns an error that it does not recognize the imports that I have in Python's project. I'm trying to use the python-shell modules.

this is TestScript.py:

import numpy as np
from algorithm.Version3.FairEnvyFreeAllocationProblem import FairEnvyFreeAllocationProblem
from algorithm.Version3.FairProportionalAllocationProblem import FairProportionalAllocationProblem

if __name__ == '__main__':
    v =  [[150., 150. ,150. ,150., 150. ,250.],
         [150., 150. ,150. ,150., 150. ,250.],
         [150., 150. ,150. ,150., 150. ,250.]]


    fpap = FairEnvyFreeAllocationProblem(v)
    ans = fpap.find_allocation_with_min_shering()
    print(ans)

and this is algoController.js:

const PythonShell = require('python-shell').PythonShell;

const test = () => {
     PythonShell.run('./src/algorithm/Version3/TestScript.py', null, function (err, results) {
        if (err) throw err;
        console.log(results);
    });
}

test()

i run : node ./src\controllers\algoController.js

and this is the error i get :

C:\Users\אליהו סתת\Desktop\fair-share-algorithm site\fair-share-algorithm\server\src\controllers\algoController.js:62
        if (err) throw err;                                                                                          
                 ^                                                                                                   

PythonShellError: ModuleNotFoundError: No module named 'algorithm.Version3'; 'algorithm' is not a package
    at PythonShell.parseError (C:\Users\אליהו סתת\Desktop\fair-share-algorithm site\fair-share-algorithm\server\node_modules\python-shell\index.js:295:21)
    at terminateIfNeeded (C:\Users\אליהו סתת\Desktop\fair-share-algorithm site\fair-share-algorithm\server\node_modules\python-shell\index.js:190:32)
    at ChildProcess.<anonymous> (C:\Users\אליהו סתת\Desktop\fair-share-algorithm site\fair-share-algorithm\server\node_modules\python-shell\index.js:182:13)
    at ChildProcess.emit (events.js:314:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
    ----- Python Traceback -----
    File "src\algorithm\Version3\TestScript.py", line 4, in <module>
      from algorithm.Version3.ConsumptionGraph import ConsumptionGraph {
  traceback: 'Traceback (most recent call last):\r\n'  
    '  File "src\\algorithm\\Version3\\TestScript.py", line 4, in <module>\r\n'  
    '    from algorithm.Version3.ConsumptionGraph import ConsumptionGraph\r\n'  
  executable: 'python',
  options: null,
  script: 'src\\algorithm\\Version3\\TestScript.py',
  args: null,
  exitCode: 1
}
PS C:\Users\אליהו סתת\Desktop\fair-share-algorithm site\fair-share-algorithm\server> node ./src\controllers\algoController.js
in test2
C:\Users\אליהו סתת\Desktop\fair-share-algorithm site\fair-share-algorithm\server\src\controllers\algoController.js:62                                     
        if (err) throw err;                                                                                                                               
                 ^                                                                                                                                        
                                                                                                                                                          
  args: null,
  exitCode: 1
}

this is where I put the python project (algorithm) in my node server:

enter image description here

CodePudding user response:

needed to remove all the 'algorithm.Version3.' from the imports in the python code.

  •  Tags:  
  • Related