undefinedfix
Sign in

Module not founderror: no module named 'pymongo'

TheAoHna edited in Sun, 26 Jun 2022

I run a single file directly, and there is no import error. In addition, it is normal for me to use mongodb in py file alone, but when I run it in a sketch project, I will say that the import failed. Why?

import json
import pymongo
from scrapy.utils.project import get_project_settings


class MyscrapyPipeline(object):

    def __init__(self):
        settings = get_project_settings()
        client = pymongo.MongoClient(host=settings['MONGODB_HOST'], port=settings['MONGODB_PORT'])
        db = client[settings['MONGODB_DBNAME']]
        self.mongo = db[settings['MONGODB_COLNAME']['taptap']]

    def process_item(self, item, spider):
        """ 处理数据 """

        for k, v in item.items():
            item[k] = ''.join(v.split()) if bool(type(v) is not list) else v

        self.mongo.insert_one(json.dumps(dict(item), ensure_ascii=False))

        return item
1 Replies
altegn0
commented on Sun, 26 Jun 2022

Do you use some kind of integrated development compiler, and there is no pymongo in your development environment for your sketch project? If the compiler is pychar or Spyder, this Q & A may help you modify the python environment: https://segmentfault.com/q/10...

lock This question has been locked and the reply function has been disabled.