-1

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.

davidism
  • 121,510
  • 29
  • 395
  • 339
user10634362
  • 549
  • 5
  • 21
  • I'd ask myself: why does the script require to be run as superuser? Especially so if it's a web server. – AKX May 20 '21 at 10:42
  • it is at R&D purpose stage right now running on Xilinx FPGA – user10634362 May 20 '21 at 13:21
  • Are you really running a Flask web server on Python on a Xilinx FPGA? – AKX May 20 '21 at 15:57
  • Sorry, mixed the device. Flask running on ARM processor, xilinx FPGA word is used as it is using to process data. Wrongly used the term. – user10634362 May 20 '21 at 22:15
  • You still don't need to run Python as root. Just make sure whatever device file represents the Xilinx is readable by a regularly user... – AKX May 21 '21 at 06:28
  • obviously, I should run my python script as `root` as I know what else I am doing there/ use there. – user10634362 May 21 '21 at 07:46
  • Obviously your possibly internet-facing server script won't ever have a vulnerability that would allow e.g. reading an arbitrary file or executing arbitrary code? :) – AKX May 21 '21 at 07:52
  • it is an R&D purpose issue so the cause of `vulnerability` is not counted here. I know what you mean. I am using here a library dedicated for xilinx FPGA which require `root` permission and I need some data from that lib so I run the flask with `root` permission. During deployment that specific lib will not be used. – user10634362 May 21 '21 at 08:12

1 Answers1

0

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
user10634362
  • 549
  • 5
  • 21