Python:ValueError:在非包中尝试相对导入

时间:2018-05-09 17:42:57

标签: python flask

我有以下包结构

enter image description here

这是我在home.py中的代码

import os

from flask import Blueprint, render_template, request, flash, url_for

from .. import db
from ..models.home import Summary
from ..forms.home import SummarizerForm
from ..processing.summarizer import Summarizer
from ..helpers import flash_errors
from ..processing.newsbot import NewsBot

home = Blueprint('home', __name__)


@home.route('/', methods=['GET', 'POST'])
def index():
    summary = None
    url = ''

    form = SummarizerForm(request.form)

    if request.method == "POST" and form.validate():
        summary = Summarizer(form.text.data, form.algorithm.data, form.length.data)
        if summary.error:
            flash(summary.error)
        else:
            source_url = form.text.data if form.text.data.startswith(('http://', 'https://')) else ''
            summary_db_entry = Summary(
                summary.bullets,
                summary.highlighted_text,
                source_url=source_url)
            db.session.add(summary_db_entry)
            db.session.commit()

            url_hash = summary_db_entry.url
            url = os.path.join(request.url, url_for('home.summary_entry', url_hash=url_hash)[1:])

    flash_errors(form)

    return render_template(
        'home/index.html',
        form=form,
        summary=summary,
        url=url
    )


@home.route('/s/<url_hash>')
def summary_entry(url_hash):
    summary = Summary.query.filter_by(url=url_hash).first_or_404()
    source_url = summary.source_url

    return render_template(
        'home/summary.html',
        summary=summary,
        source_url=source_url
    )

@home.route('/about')
@home.route('/about/')
def about():
    return render_template('home/about.html')

包中的init.py

from .views.home import home

我在init.py上遇到以下错误,该错误超出了包。

Traceback (most recent call last):
  File "/Users/johnsriskandarajah/Documents/summarizer-flask-app-master/tldrapp/__init__.py", line 35, in <module>
    from .views.home import home
ValueError: Attempted relative import in non-package

如何修复此错误?我在网上尝试了大部分解决方案但却找不到运气。

完整的项目结构 enter image description here

0 个答案:

没有答案