Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here
enter image description here

>Solution :

Replace

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

with

collection = prepare_dataframe()

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading