2013-10-31 114 views
2

前言与请求模块FLASK导入错误

您好所有我收到的时候我试图使用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) 
+0

您正在使用哪个版本的werkzeug? Mine是0.8.3,它没有这行'从urllib.request导入parse_http_list作为_parse_list_header',而不是'从urllib2导入parse_http_list作为_parse_list_header'。 – rajpy

+0

什么是最新的是...对不起,这是我关于werkzeug –

回答

1

其实2个语句是做几乎相同:

from flask import * 
import flask 

当您尝试使用在瓶中的方法,你有2种选择: 1:

from flask import * 
c = request.remote_addr 

第二:

import flask 
c = flask.request.remote_addr 

你使用import a_module注意到不同的使用from a_module import *,你可以使用该方法在module.When名称,就可以给你使用的模块的编译器,如? :a_module.method_name

+1

我不知道你是否改变了你的话,但'从瓶进口*'和'进口瓶'在技术上不做同样的事情。第一个将导入烧瓶模块,并在当前模块上为'flask'中定义的每个属性分配一个全局属性。第二个将导入'flask',并只将属性'flask'分配给当前模块。此外,虽然指出这可能对问题的作者有用,但最好在评论中指出,因为它实际上并未回答问题。 –

+0

错误导入和从瓶子导入是两件独立的事 –

+0

我知道,他们不这样做,他们只是做类似的事情。 – Liam

1

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

我想通了,因为它工作之前,然后再次工作。你的代码很好!

+0

这工作!谢谢 –