bonzanini / Book-SocialMediaMiningPython

Companion code for the book "Mastering Social Media Mining with Python"
548 stars 264 forks source link

ImportError: No module named 'twitter_client' #10

Open MarieFrantz opened 6 years ago

MarieFrantz commented 6 years ago

Hello, I know someone else brought up this issue, but I can't tell that is was resolved. I am running the code provided with the book, and continue to get this message when attempting to retrieve tweets from the timeline. I'm using python 3.6. Your assistance is much appreciated. Thank you.

bonzanini commented 6 years ago

Could you please clarify which script you're trying to run, and how you're running it? (exact sequence of commands)

MarieFrantz commented 6 years ago

Hi Marco,

Thank you soo much for writing back. I am working on a Mac, and running Jupyter Notebook. I tried running the code below using both the most recent version of Tweepy and the 3.30, to see if that would resolve the issue. I keep getting an error message stating:

No module named 'twitter_client'. This is precluding me from progressing through the book. Thanks in advance for your help.

Here is the script:

import sys import string import time from tweepy import Stream from tweepy.streaming import StreamListener from twitter_client import get_twitter_auth

class CustomListener(StreamListener): """Custom StreamListener for streaming Twitter data."""

def __init__(self, fname):
    safe_fname = format_filename(fname)
    self.outfile = "stream_%s.jsonl" % safe_fname

def on_data(self, data):
    try:
        with open(self.outfile, 'a') as f:
            f.write(data)
            return True
    except BaseException as e:
        sys.stderr.write("Error on_data: {}\n".format(e))
        time.sleep(5)
    return True

def on_error(self, status):
    if status == 420:
        sys.stderr.write("Rate limit exceeded\n".format(status))
        return False
    else:
        sys.stderr.write("Error {}\n".format(status))
        return True

def format_filename(fname): """Convert fname into a safe string for a file name.

Return: string
"""
return ''.join(convert_valid(one_char) for one_char in fname)

def convert_valid(onechar): """Convert a character into '' if "invalid".

Return: string
"""
valid_chars = "-_.%s%s" % (string.ascii_letters, string.digits)
if one_char in valid_chars:
    return one_char
else:
    return '_'

if name == 'main': query = sys.argv[1:] # list of CLI arguments query_fname = ' '.join(query) # string auth = get_twitter_auth() twitter_stream = Stream(auth, CustomListener(query_fname)) twitter_stream.filter(track=query, async=True)

On Sun, Jun 17, 2018 at 4:55 AM, Marco Bonzanini notifications@github.com wrote:

Could you please clarify which script you're trying to run, and how you're running it? (exact sequence of commands)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bonzanini/Book-SocialMediaMiningPython/issues/10#issuecomment-397867694, or mute the thread https://github.com/notifications/unsubscribe-auth/ASqvFjEpUX5C6RJXQFMaFVt40wTQ3Ho1ks5t9ieOgaJpZM4Uomh1 .

-- All the best, Marie Wallmark (503)781-9700 mariefrantz@gmail.com

bonzanini commented 6 years ago

The error says that the module twitter_client is not found: this is the file twitter_client.py that you can find in the folder Chap02-03. This file has to be in the same folder as the code you're trying to run so the import can run correctly.

Another problem is that the code is designed to run as a command-line script (i.e. from a terminal):

# make sure you're in the correct folder first
cd Chap02-03
# run the script from the command line
python twitter_streaming.py [your search keywords here]

This will open the streaming and download tweets into a file stream_[your keywords].jsonl, it will keep streaming until you kill the process. If you run this code from a notebook, the notebook will hang while the .jsonl file is being created. I'd suggest you run the streaming script from the command-line as intended, and then you can run all the analysis scripts from a jupyter notebook once the file .jsonl is created.

MarieFrantz commented 6 years ago

Marco, I am so grateful for your help. So, as you've instructed, I've run the script for the streaming file in terminal, and I am now getting an error message stating, can't find 'main' module in ' chap02-03'. Any idea what this means? I am desperate to get data from Twitter for my project. Thanks in advance for your help.

On Tue, Jun 19, 2018 at 12:19 AM, Marco Bonzanini notifications@github.com wrote:

The error says that the module twitter_client is not found: this is the file twitter_client.py that you can find in the folder Chap02-03. This file has to be in the same folder as the code you're trying to run so the import can run correctly.

Another problem is that the code is designed to run as a command-line script (i.e. from a terminal):

make sure you're in the correct folder first

cd Chap02-03

run the script from the command line

python twitter_streaming.py [your search keywords here]

This will open the streaming and download tweets into a file stream_[your keywords].jsonl, it will keep streaming until you kill the process. If you run this code from a notebook, the notebook will hang while the .jsonl file is being created. I'd suggest you run the streaming script from the command-line as intended, and then you can run all the analysis scripts from a jupyter notebook once the file .jsonl is created.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/bonzanini/Book-SocialMediaMiningPython/issues/10#issuecomment-398277338, or mute the thread https://github.com/notifications/unsubscribe-auth/ASqvFoDZxmdO-5CxjDav7--QoVXB5WU7ks5t-In4gaJpZM4Uomh1 .

-- All the best, Marie Wallmark (503)781-9700 mariefrantz@gmail.com

divyajyothii commented 5 years ago

Hello,

I am getting the "TWITTER_* environment variables not set" exception even though the variables are correct. Could you please help me with this?

Thank you