Forums

[Bottle] Error running WSGI application ImportError: cannot import name 'app' from 'app'

Hello, I've been trying to troubleshoot this error for more than a few hours and not sure where else to turn. I am new to Python in general and much more deployment. I keep getting the following error:

Error running WSGI application ImportError: cannot import name 'app' from 'app' (/home/joliver/web_apps/zendesk_ticketing/app.py) File "/var/www/joliver_pythonanywhere_com_wsgi.py", line 24, in <module> from app import app as application

I have read multiple forum posts regarding this problem but they are all referencing Flask. I am not sure how it is that I can grab or define the module name for my app so that I can reference it inside the wsgi file. Perhaps I am completely off-base here.

Project folder path: /home/joliver/web_apps/zendesk_ticketing/

Here's the code for the app.py file inside the Project folder:

import json 
from dotenv import load_dotenv
from pathlib import Path

import requests
from bottle import route, template, run, static_file, request, response

# dotenv_path = Path('path/to/.env')
# load_dotenv(dotenv_path=dotenv_path)

@route('/', method=['GET', 'POST'])
  def handle_form():
      if 'verified_email' in request.cookies:
          ask_email = False
     else:
         ask_email = True
     status = ''

if request.POST:
    # Get the form data
    # custom_fields = [{"id": 1, "value": LName}] 
    fName = request.forms.get('fName')
    LName = request.forms.get('LName')
    kvmModel = request.forms.get('kvm_model')
    orderNumber = request.forms.get('order_number')
    originalReceipt = request.forms.get('original_receipt')
    # computer = request.forms.get('computer')
    # monitor = request.forms.get('monitor')
    computer1 = request.forms.get('computer_make_1')
    computer2 = request.forms.get('computer_make_2')
    # computer3 = request.forms.get('computer_make_3')
    # computer4 = request.forms.get('computer_make_4')
    monitor1 = request.forms.get('monitor_make_1')
    monitor2 = request.forms.get('monitor_make_2')
    monitor3 = request.forms.get('monitor_make_3')
    monitor4 = request.forms.get('monitor_make_4')
    keyboardInfo = request.forms.get('keyboard_info')
    mouseInfo = request.forms.get('mouse_info')
    dockingType = request.forms.get('docking_type')

    subject = request.forms.get('subject')
    description = request.forms.get('description') + '\n\n' + 'Name: ' + fName + ' ' + LName + '\n' + 'KVM: ' + kvmModel + '\n' + 'Order: ' + orderNumber + '\n' + 'Receipt: ' + originalReceipt + '\n' + 'Keyboard: ' + keyboardInfo + '\n' + 'Mouse: ' + mouseInfo + '\n' + 'Dock: ' + dockingType + '\n' + 'Computers: ' + computer1 + ' ' + computer2 + '\n' + 'Monitors: ' + monitor1 + ' ' + monitor2 + ' ' + monitor3 + ' ' + monitor4
    if 'verified_email' in request.cookies:
        email = request.get_cookie('verified_email')
    else:
        email = request.forms.get('email')

    # Package the data for the API
    data = {'request': {'subject': subject, 'comment': {'body': description}}}
    ticket = json.dumps(data)

    # Make the API request
    user = email + '/token'
    api_token = '3Zfkrk9zEVsUtPsoYvyztjiOg6RxIXpbcHrJ6Zld'
    url = 'https://buytesmart.zendesk.com/api/v2/requests.json'
    headers = {'content-type': 'application/json'}
    r = requests.post(url, data=ticket, auth=(user, api_token), headers=headers)
    if r.status_code != 201:
        if r.status_code == 401 or 422:
            status = 'Could not authenticate you. Check your email address or register.'
            ask_email = True
        else:
            status = 'Problem with the request. Status ' + str(r.status_code)
    else:
        status = 'Ticket was created. Look for an email notification.'
        if 'verified_email' not in request.cookies:
            response.set_cookie('verified_email', email, max_age=364*24*3600)
            ask_email = False

return template('ticket_form', feedback=status, no_email=ask_email)

@route('/css/<filename>')
def send_css(filename):
       return static_file(filename, root='static/css')

 if __name__ == '__main__':
       run(host='localhost', port=8080, debug=True)

# run(host='localhost', port=8080, debug=True)
# run(host='0.0.0.0', port=8080)

Here's the code inside the wsgi file:

import bottle
import os
import sys

# add your project directory to the sys.path
# project_home = '/home/joliver/web_apps'
project_home = '/home/joliver/web_apps/zendesk_ticketing'
if project_home not in sys.path:
      sys.path = [project_home] + sys.path

# make sure the default templates directory is known to Bottle
templates_dir = os.path.join(project_home, 'views/')
if templates_dir not in bottle.TEMPLATE_PATH:
     bottle.TEMPLATE_PATH.insert(0, templates_dir)

# import bottle application
# from app import application
from app import app as application

I am not sure where the conflict resides or if I am just not declaring the module name for my app correctly. Any help would be appreciated.

I don't see any declaration of the app in your app.py pasted above, so it can't be imported from there.

Hello pafk,

I wasn't sure how to declare the module in order to export it. I was able to get it to work after adding the declaration and modifying the routes:

app = Bottle()
@app.route('/create_ticket', method=['GET', 'POST'])
@app.route('/css/<filename>')
def send_css(filename):
    return static_file(filename, root='static/css')

Now I'm having trouble with the actual submission. Thanks for the reply.

What do you mean by "actual submission"?