Forums

Python 3.5 + PRAW 3.5.0 Errors when using PA, not on local machine

I have a bot for Reddit, built on Python 3 and PRAW 3.5. Essentially, it scans new posts in a sub, sends and reads private messages, and approves or deletes posts. If it approves a post it also comments on the post and both distinguishes it as an official mod post and stickies it.

It was originally built using a base Debian install in a virtual machine, and it works fine there. Unfortunately, on PythonAnywhere (free account) I get this error when it tries to distinguish and sticky a comment:

(<class 'TypeError'>, TypeError("distinguish() got an unexpected keyword argument 'sticky'",), <traceback object at 0x7fcba6575c08>)

My code to distinguish looks like this:

if not foundComment:
    # No existing comment
    comment_response = praw_post_object.add_comment(comment_text)
    commentURL = 'https://www.reddit.com/r/{0}/comments/{1}/_/{2}'.format(cfg['subreddit'],comment_response.parent_id[3:],comment_response.id)
    getCommentGenerator = r.get_submission(commentURL)
    # Shouldn't need to do that but for some reason it wants it. test further.
    getCommentGenerator.comments[0].distinguish(sticky = True)

From the error I would assume I'm on the wrong version of PRAW, but I've checked and double-checked and everything looks correct.

Sometimes I get occasional errors like this (exc_info() captured by the logging class):

(<class 'requests.exceptions.ConnectionError'>, ConnectionError(ProtocolError('Connection aborted.', BadStatusLine("''",)),), <traceback object at 0x7f525cc90308>)

and

(<class 'praw.errors.OAuthInvalidToken'>, OAuthInvalidToken(), <traceback object at 0x7f525cc93588>)

Both of which I'm assuming are "normal" errors with any recurring calls to an API or other service issues. The bot is resilient, it sleeps a bit and tries again and that's fine. But the sticky issue I can't sort out or work around no matter what I've tried. Everything else works just fine - sending and reading PMs, reading new posts, deleting and approving posts, and adding, editing, and distinguishing comments.

I know this isn't much to go on, any help is appreciated!

How are you running the script? That is, is it from a scheduled task, in a web app, or from a console? If it's from the console, what's the exact command you're using to run it? Like you, I'm thinking that it must be some kind of weird version mismatch, and maybe there's something non-obvious and PythonAnywhere-specific going on that I might spot.

I have a custom console defined with this command:

python3.4 ~/wcb.py

Doesn't seem to be any difference between running that and starting a bash prompt and running

python3 ~/wcb.py

directly.

I just checked and it appears I have an older version of PRAW installed for python 2 vs 3, but on my test Debian install I don't have the python 2 version installed at all so I doubt it's inadvertently running the python 2 version somehow.

That all sounds fine. What do you get if you print praw.__version__ at the start of your code?

3.5.0

At least I know that part is working as expected, haha

Hmm, interesting. I've checked your account and it looks like the system image you have would have python3 mapping to Python 3.5. It also has PRAW for Python 3.4 set to 3.1, which doesn't have the sticky kwarg, and PRAW for Python 3.5 set to 3.5, which does have that kwarg.

So my guess is that when you were testing it just now, you ran it using python3, which meant that you got 3.5, which worked. But your custom console is using python3.4, which has the wrong version of PRAW.

Try changing the custom console to explicitly use the python3.5 command to run the script, and let's see if that helps.

Ah! You nailed it. I changed from 3.5 to 3.4 on that command to troubleshoot another problem I was having but never changed back. I take a bit of a "hulk smash" approach to programming, it's effective at getting work done at the expense of breadth of knowledge and I missed the sub-version granularity that can exist with packages.

Thank you for the help!

Excellent! Glad we got it sorted.