Marcello B.
Marcello B.

Reputation: 4440

FLASK Import error with request module

PREFACE

Hello all I am getting a very strange error when I am trying to use the flask module for python... First let me explain that I first started writing the program and then it stopped working NO PROBLEM My last copy should work... Right? After I tried to revert back to 2 versions before the current versions code I was still getting the same error I have done everything I can think of doing now it is time to give it to the professionals

Ok so what is happening is that when I try to run my flask applications

      Traceback (most recent call last):
    File "/Users/me/Desktop/ASL server/router.py", line 47, in <module>
      from flask import *
    File "/Library/Python/2.7/site-packages/flask/__init__.py", line 17, in <module>
      from werkzeug.exceptions import abort
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/__init__.py", line 154, in <module>
      __import__('werkzeug.exceptions')
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/exceptions.py", line 71, in <module>
      from werkzeug.wrappers import Response
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/wrappers.py", line 26, in <module>
      from werkzeug.http import HTTP_STATUS_CODES, \
    File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/http.py", line 28, in <module>
      from urllib.request import parse_http_list as _parse_list_header
  ImportError: No module named request

Currently I have tried to uninstall the module and reinstall it along with all supporting modules. The code is a reverted code that was from before the error occourd and now the error is happening again (No changes have been made to the old code) I do not know what the problem is ...

My python code is

    from flask import *
    import flask
    from flask import render_template
    import os
    import random
    import sys
    app = Flask(__name__)
    app.secret_key ="REST SECRET"
    ##############################################
    @app.errorhandler(503)                       #
    def page_not_found(e):                       #
        flash('503 Service Unavailable')         #
        return render_template('500.html'), 500  #
    @app.errorhandler(408)                       #
    def page_not_found(e):                       #
        flash('408 Request Timeout')             #
        return render_template('403.html'), 408  #
    @app.errorhandler(401)                       #
    def page_not_found(e):                       #
        flash('401- Restricted')                 #
        return render_template('403.html'), 401  #

    ##############################################
    def getip():
        c = request.remote_addr
        i = c.encode(encoding='UTF-8',errors='strict')
        ip = i.encode(encoding='UTF-8',errors='strict')

    @app.route('/')
    def home():
        ipa = getip()
        session[ipa] = ipa
        print session
        print ipa
        return render_template('index.html')


    @app.route('/donate')
    def donate():
        return render_template('donate.html')
    @app.route('/learn/fingerspell/end')
    def learnfinger():
        return render_template('Welcometofinger.html')
    @app.route('/learn/fingerspell',methods=["GET","POST"])
    def learnfingera():
        x = "a"
        if request.method == "POST":
            if request.form['next'] == "clear":
                return redirect('/learn/fingerspell/b')

        return render_template("fingerspell.html",error=x)

    @app.route('/learn/fingerspell/b',methods=["GET","POST"])
    def learnfingerb():
        x = "b"
        if request.method == "POST":
            if request.form['next'] == "clear":
                return redirect('/learn/fingerspell/c')

        return render_template("fingerspell.html",error=x)

    # And so on and on till you get to Z

    @app.route('/learn/fingerspell/z',methods=["GET","POST"])
    def learnfingerz():
        x = "z"
        if request.method == "POST":
            if request.form['next'] == "clear":
                return redirect('/learn/fingerspell/end')

        return render_template("fingerspell.html",error=x)



    @app.route('/learn/basic-words')
    def learnwordb():

        return render_template('bwordsmain.html')

    @app.route('/learn/basic-words/course')
    def learnwordb_course():
        flash("Welcome to your first lesson ")
        return 
    @app.route('/learn/basic-words/course/test/id1',methods=["GET","POST"])
    def test1baiscstart():
        score = 0
        if request.method == "POST":
            if request.form['a'] == "ASL":
                score += 1
        return render_template('test1.html')

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

    @app.route('/test/id/1',methods=["POST","GET"])
    def testabcs():
        score = 100
        missq = 0
        truea = 5
        if request.method == "POST":
            ab = request.form['qa']
            ba = request.form['qb']
            ca = request.form['qc']
            da = request.form['qd']
            ea = request.form['qe']
            a = str(ab).lower()
            b = str(ba).lower()
            c = str(ca).lower()
            d = str(da).lower()
            e = str(ea).lower()
            if True:
                if a != "asl is fun":
                    score -= 20
                    missq += 1
                elif b != "this isnt hard":
                    score -= 20
                    missq += 1
                elif c != "its fun":
                    score -= 20
                    missq += 1
                elif d != "m":
                    score -= 20
                    missq += 1
                elif e != "t":
                    score -= 20
                    missq += 1
                else:
                    flash("DEATH ERROR")
            flash("you scored "+ str(score)+"%  and missed " + str(missq) + " out of " + str(truea))
            return render_template('info.html')

        return render_template("abcstest.html")
    @app.route('/games')
    def games():
        return render_template("games.html")


    if __name__ == '__main__':
        port = int(os.environ.get('PORT', 5001))
        app.debug = True
        app.run(host='0.0.0.0',port=port)

Upvotes: 2

Views: 11504

Answers (2)

Marcello B.
Marcello B.

Reputation: 4440

Sometimes directories break... Try copying the files out of the directory and make a new directory and then rerun the server that should work.

I figured this out because it worked before and then it worked again. Your code is just fine!

Upvotes: 1

Liam
Liam

Reputation: 18

Actually the 2 statements is doing nearly the same:

from flask import *
import flask

when you try to use a method in flask, you have 2 choices: 1st:

from flask import *
c = request.remote_addr

2nd:

import flask
c = flask.request.remote_addr

Do you notice the different?When using from a_module import *, you can just use the method name in the module.When using import a_module, you have to the compiler which module you use, such as: a_module.method_name

Upvotes: 1

Related Questions