我在python-shell库方面遇到问题,也许您可以找到解决方法。
因此,在下面(test.js)运行我的JavaScript代码之后...
const {PythonShell} = require('python-shell');
var myPythonScriptPath = 'my_script.py';
var pyshell = new PythonShell(myPythonScriptPath);
// sends a message to the Python script via stdin
pyshell.send(852438003098);
pyshell.on('message', function (message) {
// received a message sent from the Python script (a simple "print" statement)
console.log('This is the response from Python: ', message);
});
// end the input stream and allow the process to exit
pyshell.end(function (err,code,signal) {
if (err) throw err;
console.log('The exit code was: ' + code);
console.log('The exit signal was: ' + signal);
console.log('finished');
console.log('finished');
});
使用此python脚本(这是“ my_script.py”,与上面的“ test.js”放在同一目录中):
import sys
from pymongo import MongoClient
# MONGODB STUFF
client = MongoClient()
db = client.TestDB
c = db.main_items
# GET INPUT FROM JAVASCRIPT HERE
query = sys.argv[1]
# SUPPOSED TO WRITE TO DATABASE HERE
c.insert_one({data: query})
运行JavaScript代码时出现此错误:
PS C:\Users\User\Desktop\App_Backend\routes> node test.js
C:\Users\User\Desktop\App_Backend\routes\test.js:20
if (err) throw err;
^
PythonShellError: AssertionError: SRE module mismatch
at PythonShell.parseError (C:\Users\User\Desktop\App_Backend\node_modules\python-shell\index.js:260:21)
at terminateIfNeeded (C:\Users\User\Desktop\App_Backend\node_modules\python-shell\index.js:139:32)
at ChildProcess.<anonymous> (C:\Users\User\Desktop\App_Backend\node_modules\python-shell\index.js:131:13)
at ChildProcess.emit (events.js:203:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:272:12)
----- Python Traceback -----
File "C:\Users\User\Desktop\App_Backend\routes\my_script.py", line 3, in <module>
from pymongo import MongoClient
File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\__init__.py", line 77, in <module>
from pymongo.collection import ReturnDocument
File "C:\ProgramData\Anaconda3\lib\site-packages\pymongo\collection.py", line 21, in <module>
from bson.code import Code
File "C:\ProgramData\Anaconda3\lib\site-packages\bson\__init__.py", line 67, in <module>
import calendar
File "C:\ProgramData\Anaconda3\lib\calendar.py", line 10, in <module>
import locale as _locale
File "C:\ProgramData\Anaconda3\lib\locale.py", line 16, in <module>
import re
File "C:\ProgramData\Anaconda3\lib\re.py", line 123, in <module>
import sre_compile
File "C:\ProgramData\Anaconda3\lib\sre_compile.py", line 17, in <module>
assert _sre.MAGIC == MAGIC, "SRE module mismatch" {
traceback: 'Traceback (most recent call last):\r\n' +
' File "C:\\Users\\User\\Desktop\\App_Backend\\routes\\my_script.py", line 3, in <module>\r\n' +
' from pymongo import MongoClient\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pymongo\\__init__.py", line 77, in <module>\r\n' +
' from pymongo.collection import ReturnDocument\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\pymongo\\collection.py", line 21, in <module>\r\n' +
' from bson.code import Code\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\bson\\__init__.py", line 67, in <module>\r\n' +
' import calendar\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\calendar.py", line 10, in <module>\r\n' +
' import locale as _locale\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\locale.py", line 16, in <module>\r\n' +
' import re\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\re.py", line 123, in <module>\r\n' +
' import sre_compile\r\n' +
' File "C:\\ProgramData\\Anaconda3\\lib\\sre_compile.py", line 17, in <module>\r\n' +
' assert _sre.MAGIC == MAGIC, "SRE module mismatch"\r\n' +
'AssertionError: SRE module mismatch\r\n',
executable: 'py',
options: null,
script: 'C:\\Users\\User\\Desktop\\App_Backend\\routes\\my_script.py',
args: null,
exitCode: 1
这是怎么回事?为什么会出现这个错误?到目前为止,我还没有找到解决方法。