简体   繁体   English

通过 NodeJs 在我的 Python 脚本中使用 Pymongo

[英]Using Pymongo in my Python script through NodeJs

I'm "spawning" a python script from my NodeJs server code using我正在使用我的 NodeJs 服务器代码“生成”一个 python 脚本

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.我的 python 代码应该处理从我的 MongoDB 集合中获取的一些数据,所以我需要访问我的数据库。 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我已经安装了pymongodnspython并且我确信这一点,因为每当我为它们中的任何一个运行安装命令时,我都会得到一个

Requirement already satisfied: dnspython in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (1.16.0)已满足要求:/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (1.16.0) 中的 dnspython

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 pymongo.errors.ConfigurationError: 必须安装“dnspython”模块才能使用 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.我意识到这是一个环境问题,因为当我在 PyCharm 中运行它时,相同的代码正确运行并成功连接到 Mongo,但我不确定如何使其在 NodeJs 子进程中工作。

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.然后再次尝试安装dnspython

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))因此,正如日志消息所报告的那样,确实安装了所需的模块,但它们被安装到 Python3.7 和 Python 3.9 的目录中(我在 PyCharm 上运行我的代码,它使用 python3.7 作为解释器,这反过来解释了它们在它的目录,我使用'pip3'安装了模块,这解释了它们在python3.9目录中(我认为))

I just needed to change 'python' to 'python3.7' to solve the problem, so the code became我只需要将'python'更改为'python3.7'即可解决问题,因此代码变为

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.如果您遇到同样的问题,请首先确保检查模块的安装位置,然后确保您使用相同版本的 python 运行脚本。

Check the comments if you wish to go through the process in more detail.如果您想更详细地完成该过程,请查看评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM