2

I am developing a quiz application in node.js. I need some python script to keep log of user,so I want to use key logger to keep monitoring the user while attempting the quiz. Here is the python keylogger script:

from pynput.keyboard import Key, Listener
import logging

log_directory = r"G:/Pythonin Node/Keylogger/key_logger/public/log_files/"

logging.basicConfig(filename = (log_directory+"keylog.txt"), level = logging.DEBUG)

def on_press(key):
    logging.info(str(key))

with Listener(on_press = on_press) as listener:
    listener.join()

Script working well when i run it in pycharm.but when I call it from node application using python-shell I found an error:

{
traceback: "Traceback (most recent call last): File "script.py", line 1, in <module> from pynput.keyboard import Key, Listener ModuleNotFoundError: No module named 'pynput' ",
executable: "py",
options: null,
script: "script.py",
args: [
"xyz",
"abc"
],
exitCode: 1
}

This is the simple json response.

Here is my node code:

app.get('/', callD_alembert);

function callD_alembert(req, res) {
    var x="xyz";
    var y="abc";
    
  var options = {
    args:
    [
      x,
      y
    ]
  }
  PythonShell.run('./script.py', options, function (err, data) {
    if (err) res.send(err);
    res.send(data.toString())
  });
}

python shell executes the simple python script in which I don't use any external package.but when I to use "pynput" package and want to import it.it gives the following error:

enter image description here

Here is also running a python interpreter: enter image description here

please help me to solve this issue.

Thank you

Hammad Ali
  • 331
  • 3
  • 12

1 Answers1

1

It looks like you are running the python interpreter in different environments.

Try adding the code below to your python script, and run it from pycharm and using PythonShell:

import sys
print(sys.executable)

If the printed paths are different, try modifying the options you pass to PythonShell, so that the path matches the one you have while running the script via pycharm:

var options = {
  // replace this with the path you got by running the script in pycharm
  pythonPath: 'path/to/python',
  args:
  [
    x,
    y
  ]
}
Thiago Barcala
  • 6,463
  • 2
  • 20
  • 23
  • Thanks for your response. yes both shows different path know I changed it but I still facing the error: Unable to import 'pynput.keyboard'. How to install this package in my script in VS-Code – Hammad Ali Jul 04 '20 at 12:02
  • this package is working well in pycharm but when I try to execute it using python-shell in node app it throw the error: Unable to import 'pynput.keyboard'. – Hammad Ali Jul 04 '20 at 12:05