ImportError: No module named views
<p>Im writing a web app in flask that displays bus stops pulled from an api. </p>

<p>i have a form on index.html where a user can input a stop number, that number gets picked up in views.py where the function also runs a task through celery to fetch the api data:</p>

<pre><code>from flask import render_template, request
from app import app

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/stopno', methods=['POST'])
def stopno():
    stopid = request.form['input']
    from app.tasks import apidata
    apidata.delay()
    return render_template('action.html')
</code></pre>

<p>here is my tasks.py:</p>

<pre><code>from celery import Celery
import json
import requests
import time

ac = Celery('tasks', broker='amqp://localhost')

@ac.task(name='tasks.apidata')
def apidata():
    from views import stopno
    api = '*apiurl*:' + str(stopid)
    saveinfo = 'static/stop' + str(stopid)+ '.json'

    r = requests.get(api)
    jsondata = json.loads(r.text)

    with open(saveinfo, 'w') as f:
        json.dump(jsondata, f)
</code></pre>

<p>I am importing stopno from views into tasks so i can search the api for the specified stop, currently when a user inputs a stop number the action.html loads fine and displays the stop number the user inputs but no new file or data is created for the stop number and celery throws an error saying</p>

<pre><code>ImportError: No module named views
</code></pre>

<p>structure of my project is</p>

<pre><code>|____run.py
|____app
| |______init__.py
| |____views.py
| |____tasks.py
| |____static
| | |____json
| |____templates
| | |____action.html
| | |____index.html
</code></pre>

<p>You are trying to do a relative import but you are not making it <em>explicitly</em> relative, which may or may not work, and which can cause unexpected errors. You should make your import explicitly relative with:</p>

<pre><code>from .views import stopno
</code></pre>

<p>This way you don't have to worry about reproducing the entire path to the module.</p>

python module celery python-import celery-task