Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取错误:无法从部分初始化的模块导入名称“DB”。没有找到循环导入问题_Python_Flask_Flask Sqlalchemy_Spacy - Fatal编程技术网

Python 获取错误:无法从部分初始化的模块导入名称“DB”。没有找到循环导入问题

Python 获取错误:无法从部分初始化的模块导入名称“DB”。没有找到循环导入问题,python,flask,flask-sqlalchemy,spacy,Python,Flask,Flask Sqlalchemy,Spacy,我正在为学校做作业 但是当我尝试运行flask应用程序时,我得到了某种循环导入错误。我一直在尝试回去,一步一步地删除东西,同时仍然保持我的项目功能。我碰壁了: 用法:main.py运行[选项] 错误:导入“Help.app”时,引发了导入错误: 回溯最近的调用last:FileUsage:main.py运行[选项] 错误:导入“Help.app”时,引发了导入错误: 回溯最近的调用上次:文件 /home/chris/Help/venv/lib/python3.8/site-packages/fl

我正在为学校做作业

但是当我尝试运行flask应用程序时,我得到了某种循环导入错误。我一直在尝试回去,一步一步地删除东西,同时仍然保持我的项目功能。我碰壁了:

用法:main.py运行[选项] 错误:导入“Help.app”时,引发了导入错误: 回溯最近的调用last:FileUsage:main.py运行[选项]

错误:导入“Help.app”时,引发了导入错误:

回溯最近的调用上次:文件 /home/chris/Help/venv/lib/python3.8/site-packages/flask/cli.py,第行 256,在定位应用程序中

__import__(module_name)   File "/home/chris/Help/__init__.py", line 1, in <module>
from .app import create_app
Twitter.py文件:

"""Retrieve tweets and users then create embeddings and populate DB"""
from os import getenv
import tweepy
import spacy
from .models import DB, Tweet, User

# TODO - Don't include raw keys and tokens (create .env file)
TWITTER_API_KEY = getenv("TWITTER_API_KEY")
TWITTER_API_SECRET_KEY = getenv("TWITTER_API_SECRET_KEY")
TWITTER_OAUTH = tweepy.oAuthHandler(TWITTER_API_KEY, TWITTER_API_SECRET_KEY)
TWITTER = tweepy.API(TWITTER_OAUTH)

# NLP model
nlp = spacy.load("my_model")


def vectorize_tweet(tweet_text):
    return nlp(tweet_text).vector


def add_or_update_user(username):
    try:
        twitter_user = TWITTER.get_user(username)
        db_user = (User.query.get(twitter_user.id)) or User(
            id=twitter_user.id, name=username
        )
        DB.session.add(db_user)
        tweets = twitter_user.timeline(
            count=200, exclude_replies=True, include_rts=False, tweet_mode="extended"
        )

        if tweets:
            db_user.newest_tweet_id = tweets[0].id

        for tweet in tweets:
            vectorized_tweet = vectorize_tweet(tweet.full_text)
            db_tweet = Tweet(id=tweet.id, text=tweet.full_text, vect=vectorized_tweet)
            db_user.tweets.append(db_tweet)
            DB.session.add(db_tweet)

        DB.session.commit()

    except Exception as e:
        print(f"Error processing {username}: {e}")
        raise e
models.py文件:

""""SQLAlchemy models and utility functions for Twitoff Application"""
from flask_sqlalchemy import SQLAlchemy
from .twitter import add_or_update_user


DB = SQLAlchemy()


class User(DB.Model):
    """Twitter User table that will correspond to tweets - SQLAlchemy syntax"""
    id = DB.Column(DB.BigInteger, primary_key=True)
    name = DB.Column(DB.String, nullable=False)
    newest_tweet_id = DB.Column(DB.BigInteger)

    def __repr__(self):
        return f"<User:{self.name}>"


class Tweet(DB.Model):
    """tweet text data - associated with User table"""
    id = DB.Column(DB.BigInteger, primary_key=True)
    text = DB.Column(DB.Unicode(290))
    vect = DB.Column(DB.PickleType, nullable=False)
    user_id = DB.Column(DB.BigInteger, DB.ForeignKey("user.id"), nullable=False)
    user = DB.relationship("User", backref=DB.backref('tweets', lazy=True))

    def __repr__(self):
        return f"<Tweet: {self.text}"


def insert_example_users():
    """We will get an error if we run this twice without dropping & creating"""
    users = ["elonmusk", "geoffkeighley", "iamjohnoliver", "neiltyson"]
    for user in users:
        DB.session.add(add_or_update_user(user))
    DB.session.commit()

您可以将应用程序实例放置在app.py中而不是_init__uuuuuuuuuuu.py中。该实例应该是WSGI可调用的,而不是包标识符

""""SQLAlchemy models and utility functions for Twitoff Application"""
from flask_sqlalchemy import SQLAlchemy
from .twitter import add_or_update_user


DB = SQLAlchemy()


class User(DB.Model):
    """Twitter User table that will correspond to tweets - SQLAlchemy syntax"""
    id = DB.Column(DB.BigInteger, primary_key=True)
    name = DB.Column(DB.String, nullable=False)
    newest_tweet_id = DB.Column(DB.BigInteger)

    def __repr__(self):
        return f"<User:{self.name}>"


class Tweet(DB.Model):
    """tweet text data - associated with User table"""
    id = DB.Column(DB.BigInteger, primary_key=True)
    text = DB.Column(DB.Unicode(290))
    vect = DB.Column(DB.PickleType, nullable=False)
    user_id = DB.Column(DB.BigInteger, DB.ForeignKey("user.id"), nullable=False)
    user = DB.relationship("User", backref=DB.backref('tweets', lazy=True))

    def __repr__(self):
        return f"<Tweet: {self.text}"


def insert_example_users():
    """We will get an error if we run this twice without dropping & creating"""
    users = ["elonmusk", "geoffkeighley", "iamjohnoliver", "neiltyson"]
    for user in users:
        DB.session.add(add_or_update_user(user))
    DB.session.commit()
from .app import create_app

APP = create_app()