Sekmani52
Sekmani52

Reputation: 55

unable to use pymongo in ubuntu 20

I try to insert some data in mongoDB using Python3, so I install the pymongo library using this command pip install pymongoand 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:

enter image description here

However when I try to use python3 directly from the terminal and I write import pymongo, nothing happened as shown the screenshot below:

enter image description here

I'm using Ubuntu 20 and Python 3.8, and the script saved in my desktop with the name example4_DB.pyalso 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

Answers (2)

Dan D.
Dan D.

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

Belly Buster
Belly Buster

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

Related Questions