Home > Blockchain >  Data loading on MongoDB server works from jupyter notebook but not from script
Data loading on MongoDB server works from jupyter notebook but not from script

Time:02-28

I have a dataset stored in a json file and I need to upload it to a MongoDB server. Everything works fine if I upload the data using a Jupyter notebook, but not if I use a python script instead. The code is exactly the same. How do you suggest fixing this?

Here is the code:

import pandas as pd
import pymongo
from pymongo import MongoClient
import json
import DNS

# Function to upload the dialogue lines to MongoDB Server
def prepare_dataframe():
    dialogue_edited = pd.read_json("5lines1response_random100from880_cleaned.json")
    dialogue_edited.reset_index(inplace=True)
    data_dict = dialogue_edited.to_dict("records")# Insert collection
    # To communicate with the MongoDB Server
    cluster = MongoClient()
    db = cluster['DebuggingSystem']
    collection = db['MCS_dialogue']
    collection.insert_many(data_dict)
    return collection

if __name__ == '__main__':
    collection = prepare_dataframe()

Here is a screenshot of the python script and of the jupyter notebook. I'm running the notebook using Visual Studio.

enter image description here enter image description here

CodePudding user response:

Replace

if __name__ == '__main__':
    collection = prepare_dataframe()

with

collection = prepare_dataframe()

and try runnning your script. __main__ explained here pretty well.

  • Related