Skip to main content
 首页 » 编程设计

python之请求模块的 FLASK 导入错误

2024年11月01日15zhujiabin

前言

大家好,当我尝试将 flask 模块用于 python 时,我遇到了一个非常奇怪的错误......首先让我解释一下,我首先开始编写程序然后它停止工作没问题我的最后一个副本应该工作......对吧?在我尝试恢复到当前版本代码之前的 2 个版本之后,我仍然遇到同样的错误我已经做了我能想到的所有事情,现在是时候把它交给专业人士了

好的,当我尝试运行我的 flask 应用程序时发生的事情

      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) 

请您参考如下方法:

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

from flask import * 
import flask 

当你尝试在 Flask 中使用一个方法时,你有 2 个选择: 第一:

from flask import * 
c = request.remote_addr 

第二个:

import flask 
c = flask.request.remote_addr 

你注意到不同了吗?当使用from a_module import *时,你可以直接使用模块中的方法名。当使用import a_module时,你必须compiler 你用的是哪个模块,比如:a_module.method_name