【问题标题】:FLASK Import error with request module请求模块的烧瓶导入错误
【发布时间】:2013-10-31 04:35:36
【问题描述】:

前言

大家好,当我尝试使用 python 的烧瓶模块时,我遇到了一个非常奇怪的错误...首先让我解释一下,我首先开始编写程序,然后它停止工作没有问题我的最后一个副本应该工作......对吧?在我尝试恢复到当前版本代码之前的 2 个版本之后,我仍然遇到相同的错误

好的,当我尝试运行烧瓶应用程序时发生了什么

      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

目前我已尝试卸载该模块并重新安装它以及所有支持模块。该代码是错误发生之前的还原代码,现在错误再次发生(未对旧代码进行任何更改)我不知道问题是什么......

我的python代码是

    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)

【问题讨论】:

  • 您使用的是哪个版本的 werkzeug?我的是 0.8.3,它没有 from urllib.request import parse_http_list as _parse_list_header 这一行,而是有 from urllib2 import parse_http_list as _parse_list_header
  • 最新的是什么......对不起,这就是我关于 werkzeug 的全部内容

标签: python flask runtime-error


【解决方案1】:

实际上这两条语句的作用几乎相同:

from flask import *
import flask

当您尝试在烧瓶中使用方法时,您有 2 个选择: 第一个:

from flask import *
c = request.remote_addr

第二个:

import flask
c = flask.request.remote_addr

你注意到有什么不同吗?当使用from a_module import *时,你可以只使用模块中的方法名。当使用import a_module时,你必须要编译器你使用哪个模块,例如:a_module.method_name

【讨论】:

  • 我不确定你是否说错了话,但from flask import *import flask 在技术上不会做同样的事情。第一个将导入烧瓶模块并在当前模块上为flask 中定义的每个属性分配一个全局属性。第二个将导入flask 并仅将属性flask 分配给当前模块。此外,虽然指出这一点可能对问题的作者有用,但最好在评论中指出,因为它实际上并没有回答问题。
  • 错误导入和从烧瓶导入是两个不同的东西
  • 我知道,他们不做同样的事情,他们只是做类似的事情。
  • 是的,任何看过程序设计的人都知道,但那篇文章没有受过任何教育。
【解决方案2】:

有时目录会中断...尝试将文件复制出目录并创建一个新目录,然后重新运行应该可以工作的服务器。

我发现了这一点,因为它之前有效,然后又有效。你的代码很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 2015-05-07
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多