0

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.

Bassam
  • 57
  • 8
  • Does the `python` command point to the `python3.9` installation where you installed the packages? – rdas Nov 08 '20 at 14:50
  • @rdas you mean in the ``spawn``? How can I check? – Bassam Nov 08 '20 at 14:53
  • Assuming spawn uses the default shell (not familiar with node.js), you should be able to check with `which python` – rdas Nov 08 '20 at 15:00
  • @rdas it returned ``/usr/bin/python``, does this mean it's not using Python3 to run scripts? I'm on Mac and I've read somewhere that it uses python2 to run some of its programs so I dont think changing the default to python3 would be wise. – Bassam Nov 08 '20 at 15:12
  • `/usr/bin/python` is probably a link to something else. Do a `ls -l /usr/bin/python` to check – rdas Nov 08 '20 at 15:13
  • Or just do `python --version` to check the version – rdas Nov 08 '20 at 15:14
  • ah my bad. ``python --version`` returned **Python 2.7.16**, and ``ls -l /usr/bin/python`` returned **../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7**. It's clearly pointing to the directory of version 2, is there a way to make it point to the directory given in the log message I mentioned in the Q? – Bassam Nov 08 '20 at 15:17
  • 1
    Better to change your spawn to use `python3.9` instead – rdas Nov 08 '20 at 15:18

2 Answers2

0

Try running:

sudo pip install --upgrade pip` or `python -m pip install --upgrade pip

in your command line.

And then try to install dnspython again.

Shaido
  • 27,497
  • 23
  • 70
  • 73
  • when I first ran the command it returned an error ``Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip-19.0.3-py2.7.egg/EGG-INFO/PKG-INFO' Consider using the `--user` option or check the permissions.`` I added "--user" after the "install" and pip upgraded successfully, but when I ran ``pip3 install dnspython`` it returned the same message I mentioned in the post. – Bassam Nov 08 '20 at 15:10
0

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.

Bassam
  • 57
  • 8