Issue
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 pyfile_name.py
is causing the problem.
I am using it in a Flaks environment. Below I am giving the example code.
mongo_data.py
from mongoengine import *
from random import random, randint
connect(
db='db_name',
host='localhost',
port=27017
)
class Mongo_User(Document):
user_id = SequenceField(primary_key=True)
json_data = DictField()
meta = {'collection': "col_name"}
app.py
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/")
@app.route('/get_json_for_form')
def parse_get_json_for_form(name=None):
user = Mongo_User()
json_data = {"name":"xyz"} # sample data
user.json_data = json_data
user.save()
return jsonify(json_data)
In my case, app.py
must be run with sudo python3 app.py
and which is causing the problem I guess. Any idea to overcome the problem is highly appreciable.
Solution
Got the answer by following this comment. Just used
sudo -E python3 app.py
Another way has also found but don't think as a recommended one
sudo python3 -m pip install mongoengine
Answered By - user10634362
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.