I'm "spawning" a python script from my NodeJs server code using
var spawn = require('child_process').spawn;
var python_process = spawn('python', ['server/matcher/matcher.py'])
My python code is supposed to process some data fetched from my MongoDB collection so I need to access my database. I have already installed pymongo and dnspython and I'm sure of that because whenever I run the installation command for either of them I get a
Requirement already satisfied: dnspython in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (1.16.0)
However, whenever I try to run my code I get the error
pymongo.errors.ConfigurationError: The "dnspython" module must be installed to use mongodb+srv:// URIs
I access my database as I usually do and as instructed in the documentation
from pymongo import MongoClient
client = MongoClient(<connection string>)
db = client.<db name>
users = db.users.find({})
I realize this is an environment issue because the same code ran correctly and connected to Mongo successfully when I ran it in PyCharm, but I'm not sure how to make it work in the NodeJs child process.
So as the log message reported, the required modules were indeed installed, but they were installed to the directory of Python3.7 and Python 3.9 (I ran my code on PyCharm which used python3.7 as an interpreter which in turn explains them being in its directory, and I installed the modules using 'pip3' which explains them being in python3.9 directory (I think))
I just needed to change 'python'
to 'python3.7'
to solve the problem, so the code became
var spawn = require('child_process').spawn;
If you ever face the same problem, first make sure to check where the modules are installed, then make sure you're running your script using the same version of python.
Check the comments if you wish to go through the process in more detail.