J
J
Jekson2019-03-04 11:19:04
Python
Jekson, 2019-03-04 11:19:04

Why doesn't RQ see the module?

Good day. I understand using RQ in conjunction with Flask. Inside the index function, the run function imported from another true.detect.py module is called as a separate task . I ran into an import problem. I am getting an error:

11:00:50 default: true.detect.run('/home/y700/projects/ultratrue/app/uploads/honey-bee-2-1qf37bs.jpg') (1928a0a3-2051-4951-a57b-dfc2c4992546)
11:00:50 ModuleNotFoundError: No module named 'true'

Directory structure:
project
    -true
        --detect.py
        --other files
    -app
        --app.py
        --main.py

The app.py file looks like this:
basedir = os.path.abspath(os.path.join(os.path.dirname( __file__ )))
app = Flask(__name__)
app.config.from_object(Configuration)

# Added true folder
base = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..'))
sys.path.append(base)
from true.detect import run

#Connect to redis DB
from redis import Redis
from rq import Queue

q = Queue(connection=Redis())

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        .......upload file logic......
            img = (os.path.join(app.config['UPLOAD_FOLDER'], filename))
            job = q.enqueue(run, img)
            if job.is_finished:
                return redirect(url_for("result", count=count))
            else:
                return "Wait"
    return render_template("index.html")

If you do not use the task queue and replace
job = q.enqueue(run, img)
if job.is_finished:
    return redirect(url_for("result", count=count)) 
else:
    return "Wait"

to a standard function call
count = run(img)
return redirect(url_for("result", count=count))

That all is fine imported and works.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question