#!/usr/bin/env python3

import sys
import os
import analyzer
import helpers
from termcolor import colored

def main():

    if len(sys.argv) != 2:
        sys.exit("Usage ./tweets user")

    positives = os.path.join(sys.path[0], "positive-words.txt")
    negatives = os.path.join(sys.path[0], "negative-words.txt")

    tweet_analyzer = analyzer.Analyzer(positives, negatives)

    user = sys.argv[1].replace("@","")
    Tweets = helpers.get_user_timeline(user)
    score = 0

    for Tweet in Tweets:
        score = tweet_analyzer.analyze(Tweet)

        if score > 0.0:
            print(colored("{} {}",score, Tweet, "green"))
        elif score < 0.0:
            print(colored("{} {}",score, Tweet, "red"))
            print(colored("{} {}",score, Tweet, "yellow"))

        score = 0

if __name__ == "main":

My code doesn't print any output and I am not able to figure out why. Any help would be appreciated.

  • Is Tweets containing anything? If get_user_timeline fails, it probably returns None
    – Blauelf
    Feb 23, 2017 at 16:42
  • But I tried with many screen names. Nothing
    – user34304
    Feb 23, 2017 at 16:55
  • Followed the steps to import the twitter API key/secret? The environment variables are probably there only for the current terminal, so you might need to repeat the export step.
    – Blauelf
    Feb 23, 2017 at 17:21
  • Just tried, throws an error message then. Solution is so much simpler, so simple that I did not see it before :(
    – Blauelf
    Feb 23, 2017 at 17:33

1 Answer 1


A very small error, you somehow lost the double underscores around main:

if __name__ == "__main__":

Also, using colored wrong. Should use format, like

print(colored("{} {}".format(score, Tweet), "yellow"))

For some reason, in my code, before the score is compared, there is a line

text = "{0:3d} {1}".format(score, tweet)

(not sure where I found that, maybe already was there) and then

print(colored(text, "yellow"))


  • Still throws up some kind of keyerror
    – user34304
    Feb 23, 2017 at 17:54
  • I think the way I'm using the colored function is wrong. Could you take a look? TIA
    – user34304
    Feb 23, 2017 at 18:01
  • I don't know if I've touched that part at all in my code, but found some format string there, added it to my answer. 3d means integer in decimals, if less than three characters, then padded with spaces.
    – Blauelf
    Feb 23, 2017 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .