cannot import mongoengine from a python file which require SUDO to execute


I want to import mongoengine in a python file which requires sudo to execute but I am getting the following error ModuleNotFoundError: No module named 'mongoengine'. I have tried the following:
1/ In terminal I have opened python shell and written pip show mongoengine and it can detect the version and location which means that library is installed correctly.
2/ I have tried to run the code by removing the sudo required part then also code has executed.
Which strength the point that using sudo python3 is causing the problem.
I am using it in a Flaks environment. Below I am giving the example code.

from mongoengine import *
from random import random, randint


class Mongo_User(Document):
    user_id = SequenceField(primary_key=True)
    json_data = DictField()
    meta = {'collection': "col_name"}

from flask import Flask, render_template, request, Response
from flask import jsonify
from mongo_data import *

app = Flask(__name__)
app.static_folder = os.path.abspath("templates/static/")

def parse_get_json_for_form(name=None):
    user = Mongo_User()
    json_data = {"name":"xyz"} # sample data
    user.json_data = json_data
    return jsonify(json_data)

In my case, must be run with sudo python3 and which is causing the problem I guess. Any idea to overcome the problem is highly appreciable.


Got the answer by following this comment. Just used

sudo -E python3

Another way has also found but don’t think as a recommended one

sudo python3 -m pip install mongoengine

Answered By – user10634362

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published