Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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 名称错误:全局名称';烧瓶';没有定义_Python_Twitter_Routes - Fatal编程技术网

Python 名称错误:全局名称';烧瓶';没有定义

Python 名称错误:全局名称';烧瓶';没有定义,python,twitter,routes,Python,Twitter,Routes,我对Python web编程相当陌生。请您帮助我处理运行测试应用程序时遇到的错误。正在尝试从Powershell运行命令python run.py。它给出了标题中给出的错误 run.py Config.py init.py 从flask导入flask、json、请求、呈现模板 进口粗花呢 app=烧瓶(名称) app.config.from_对象('config')) auth=tweepy.OAuthHandler(app.config['TWITTER\u CONSUMER\u KEY',a

我对Python web编程相当陌生。请您帮助我处理运行测试应用程序时遇到的错误。正在尝试从Powershell运行命令python run.py。它给出了标题中给出的错误

run.py Config.py init.py
从flask导入flask、json、请求、呈现模板
进口粗花呢
app=烧瓶(名称)
app.config.from_对象('config'))
auth=tweepy.OAuthHandler(app.config['TWITTER\u CONSUMER\u KEY',app.config['TWITTER\u CONSUMER\u SECRET']))
auth.set_access_token(app.config['TWITTER_access_token'],app.config['TWITTER_access_token\u SECRET']))
tweepy_api=tweepy.api(auth)
def get_推文(用户名):
tweets=tweepy\u api.user\u时间线(screen\u name=username)
返回[{'tweet':t.text,'created_at':t.created_at,'username':username,'headshot_url':t.user.profile_image_url}
对于tweets中的t]
@app.route(“/tweet-harvester/”)
def推文(用户名):
返回flask.render_模板('tweets.html',tweets=get_tweets(用户名))
tweets.html-仅粘贴标题和正文部分的相关部分
Tweet收割机
高频收割机
{tweets%中的tweet为%1}
{%endfor%}

我猜是因为您使用的渲染模板不正确。 它应该是flask.render_template()

或者(更好)以您的方式使用它,您只需要将其作为

from flask import render_template

您尚未从flask导入渲染模板。您需要在使用之前导入它

from flask import render_template

谢谢你的建议。已修改代码以包括导入渲染模板。init.py文件现在看起来像-从flask导入flask、json、request、render_模板导入tweepy,但问题仍然存在。@AshwinKumar似乎您仍然没有修改init.py。如果是,也要更新你的帖子。如果只是尝试一下,请尝试
return flask.render_template('tweets.html',tweets=get_tweets(username))
Have updated init。请看一下这篇文章,看看它现在的样子。通过这些更新,我现在得到了错误“NameError:未定义全局名称‘flask’。@AshwinKumar这太奇怪了!尝试一个基本的简单烧瓶应用程序(非常小),看看它是否仍然存在。
from flask import Flask, json, request, render_template
import tweepy
app = Flask(__name__)
app.config.from_object('config')
auth = tweepy.OAuthHandler(app.config['TWITTER_CONSUMER_KEY'],app.config['TWITTER_CONSUMER_SECRET'])
auth.set_access_token(app.config['TWITTER_ACCESS_TOKEN'],app.config['TWITTER_ACCESS_TOKEN_SECRET'])
tweepy_api = tweepy.API(auth)

def get_tweets(username):
tweets = tweepy_api.user_timeline(screen_name=username) 
return [{'tweet': t.text,'created_at': t.created_at,'username': username, 'headshot_url': t.user.profile_image_url} 
for t in tweets]

@app.route('/tweet-harvester/<string:username>')
def tweets(username):
return flask.render_template('tweets.html', tweets=get_tweets(username))
<title>Tweet Harvester</title>
<body>
<div class="container">
    <h1 class="p-3">Tweet Harvester</h1>
    {% for tweet in tweets %}
    <div class="list-group">
        <a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
          <div class="d-flex w-100 justify-content-between">
            <img src="{{tweet.headshot_url}}" class="w-12 p-1 float-left image-thumbnail">
            <h5 class="ml-10 w-75 mb-1">{{ tweet.tweet }}</h5>
            <small>{{ tweet.created_at }}</small>
          </div>
        </a>
    </div>
{% endfor %}
from flask import render_template
from flask import render_template