While trying to send/receive data using native messaging between javascript and python for a firefox extension I keep running into the following error in the browser console-
stderr output from native app classifier: ModuleNotFoundError: No module named 'nltk'
I have installed nltk in my pycharm virtual environment. I want to use nltk for some text processing in the python file used for native messaging. I dont get any error messages for other packages like sys, json, struct. But I get error messages for nltk, keras. But there is no error for pandas!!
Native messaging works fine when I dont import nltk into python.
Following is the python code. I have not shared nltk part of the code here
import sys
import json
import struct
import nltk
class Informationtransmission:
def getMessage(self):
rawLength = sys.stdin.buffer.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
return json.loads(message)
def encodeMessage(self, messageContent):
encodedContent = json.dumps(messageContent).encode('utf-8')
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}
def sendMessage(self, encodedMessage):
sys.stdout.buffer.write(encodedMessage['length'])
sys.stdout.buffer.write(encodedMessage['content'])
sys.stdout.buffer.flush()
x = Informationtransmission()
receivedMessage = x.getMessage()
if receivedMessage:
x.sendMessage(x.encodeMessage(receivedMessage))
js file
function logTabs(tabs) {
let tab = tabs[0];
port.postMessage(tab.url);
}
function listTabs() {
browser.tabs.query({currentWindow: true, active: true}).then(logTabs, console.error);
}
let port = browser.runtime.connectNative("classifier");
document.addEventListener("click", (e) => {
if (e.target.id === "url") {
listTabs();
}
})
port.onMessage.addListener((response) => {
console.log("Received: " + response);
});
addon manifest.json file-
{
"browser_specific_settings": {
"gecko": {
"id": "test@example.org",
"strict_min_version": "58.0a1"
}
},
"manifest_version": 2,
"name": "classifier",
"version": "1.0",
"description": "Classifies",
"background": {
"scripts": ["popup.js"]
},
"browser_action":{
"browser_style": true,
"default_icon":{
"48":"/icon.svg"
},
"default_title":"classifier",
"default_popup":"/popup.html"
},
"permissions": [
"tabs",
"activeTab",
"scripting",
"nativeMessaging"
]
}
following are the bat and json file for native app-
@echo off
call python -u "E:\proj\send_recieve_info.py"
{
"name": "classifier",
"description": "host for native messaging",
"path": "E:\\proj\\calltoscript.bat",
"type": "stdio",
"allowed_extensions": ["test@example.org"]
}
Actually I got it. The path that python was pointing to did not have nltk and keras. The plugin was using python from system path not from the virtual environment as I thought.