简体   繁体   English

Python 2.7.3 Flask ImportError:未命名模块

[英]Python 2.7.3 Flask ImportError: No module named

I have a Flask application with this error: 我有一个带有此错误的Flask应用程序:

ImportError: No module named annotaria, referer: http://ltw1413.web.cs.unibo.it/

So, my root in web server is: 因此,我在Web服务器中的根目录是:

/home/web/ltw1413/html / home / web / ltw1413 / html

Inside html folder I have: 在html文件夹中,我有:

  • One folder named "annotaria 一个文件夹名为“ annotaria
  • One file .wsgi named "wsgi.wsgi" 一个名为“ wsgi.wsgi”的文件.wsgi

My file .wsgi is: 我的文件.wsgi是:

import sys
sys.path.insert(0, '/home/web/ltw1413/html')
from annotaria import app as application

Inside my folder "annotaria" I have: 在我的文件夹“ annotaria”中,我有:

  • "Static" folder : inside stylesheet and js “静态”文件夹:在样式表和js中
  • "Templates" folder : inside html “模板”文件夹:在html内
  • "run.py" : file python where I have my application “ run.py”:我在其中拥有应用程序的python文件

run.py is this: run.py是这样的:

from pydoc import html
from annotaria import app
from flask import Flask, render_template, redirect, request
import json
import urllib2
import urlparse
import re
import string
import os
from SPARQLWrapper import SPARQLWrapper, JSON
from rdflib import Graph, BNode, Literal, Namespace
from time import strftime
import urllib2
import BeautifulSoup

app = Flask(__name__)

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


@app.route('/index.html')
def reloader():
    return render_template('index.html')


# other app.route()...

if __name__ == '__main__':
    app.run(debug=True)

How can I find a solution? 我如何找到解决方案? Where is my error? 我的错误在哪里?

Lots of errors here... annotaria is not found on path (that is why it says... well.. exactly that). 这里有很多错误...在路径上找不到annotaria (这就是为什么它说...好吧..正是这样)。

In your code you also redefine app : you import it from annotaria and then redefine it in app = Flask(.. . 在代码中,您还重新定义了app :从annotaria导入它,然后在app = Flask(..重新定义它。

Others mentioned individual errors, but it would be helpful to understand the big picture. 其他人提到了个别错误,但有助于您了解全局。 First of all, let's assume the following structure: 首先,让我们假设以下结构:

/home/web/ltw1413/html
- wsgi.wsgi
- annotaria/
  - __init.py__
  - run.py
  - static/
  - templates

Like Klaus mentioned, you need __init__.py to tell Python that annotaria is a valid package. 就像提到的Klaus一样,您需要__init__.py来告诉Python annotaria是有效的软件包。 But then your wsgi.wsgi file needs to import the app from the run module: 但是然后,您的wsgi.wsgi文件需要从运行模块导入应用程序:

from annotaria.run import app as application

You also need to remove this unnecessary import from run.py , since you instantiate a new application: 您还需要从run.py删除此不必要的导入,因为您实例化了一个新应用程序:

from annotaria import app

Because there is no application to import, you instantiate a new Flask application. 因为没有要导入的应用程序,所以您实例化了一个新的Flask应用程序。

Finally, make sure that the app runs manually before you start deploying it. 最后,在开始部署应用程序之前,请确保该应用程序手动运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM