Reputation: 55
I try to insert some data in mongoDB using Python3, so I install the pymongo library using this command pip install pymongo
and I follow every step from this link, finally I write this script below:
import pymongo
from pymongo import MongoClient
client = MongoClient('localhost')
print (client)
db = client.clients
db.clients.count()
clients = db.clients
clients.find()
But when I execute the script, the process finish and show me this error:
However when I try to use python3 directly from the terminal and I write import pymongo
, nothing happened as shown the screenshot below:
I'm using Ubuntu 20 and Python 3.8, and the script saved in my desktop with the name example4_DB.py
also I create the script using the terminal by tapping this command: nano example4_DB.py
and this command to execute the script: sudo python3 example4_DB.py
Upvotes: 0
Views: 303
Reputation: 8567
Install pymongo
under current user:
pip3 install --user pymongo
And run the Python file under current user, too:
python3 example4_DB.py
Upvotes: 1
Reputation: 8814
Ideally you shouldn't run python using sudo
; as it will not necessarily pick up the installed modules created using pip
running under your own user account.
Try just running:
python3 example4_DB.py
Upvotes: 1