Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

PRAW: Reddit API sample code not working in Workspaces - Am I missing something?

I've been playing around with using APIs in Python and wanted to test using the Reddit API, I came across the Python Wrapper A.K.A PRAW. I followed the instructions in the latest release Docs:

http://praw.readthedocs.org/en/stable/

I installed it in the workspace using

pip install praw

I then tested the sample code given.

import praw

r = praw.Reddit(user_agent='my_cool_application')
submissions = r.get_subreddit('opensource').get_hot(limit=5)
[str(x) for x in submissions]

However, it came back with an Error saying the following.

Traceback (most recent call last):                                                                  
  File "praw.py", line 1, in <module>                                                               
    import praw                                                                                     
  File "/home/treehouse/workspace/praw.py", line 3, in <module>                                     
    r = praw.Reddit(user_agent='comment_puller')                                                    
AttributeError: 'module' object has no attribute 'Reddit' 

Not sure why thats happening but any ideas that could shed some light on this would be hugely appreciated, cheers guys!

I did as you said in a workspace (added a print around that last line) and got this as the output:

['12 :: Armada - new Docker orchestration tool', '2 :: Italian military to switch to LibreOffice and
 ODF', '37 :: Liri Browser - Open-Source, Cross-Platform and Material Design for Linu...', '20 :: Go
ogle to release open source rival to Facebook\'s "Instant Articles"', "24 :: Hi, I'm lost in my life
 and I want to try contribution to the open sour..."]   

I have just tried that and I got the same error. Could you post your code please Myers?

3 Answers

Well I installed it all and it worked.

  1. I started with Python Update ( :) ) -> I downloaded the source and compiled it (I deleted all the old versions and created a symlink in /usr/bin for the new python executable) or you can follow the Walkthrough in the link (easier is source code way;))

  2. For pip you can either use: $ pip install praw or $ easy_install praw .

  3. open up python in terminal and copy/paste the 5 lines

  4. and it works.

I also tried in the workspaces and it worked for me.

So no idea what's going on at your place :) Maybe open a fresh Workspace and try again?

Let me know if anything helped you. If not well figure out this one together.

wow, thanks for your help man! Much appreciated.

I'm at work at the moment but I'll try these when I get back and keep you updated.

Thanks again!

UPDATE:

Tried opening a new workspace and reinstalling PRAW using pip3 command, that worked fine. Success, it worked this time!

import praw

r = praw.Reddit(user_agent='my_cool_application')
submissions = r.get_subreddit('learnpython').get_hot(limit=5)
print([str(x) for x in submissions])

Which gave me the following output:

["6 :: It's been 3 days and I just couldn't understand OOP with Python.", '4 :: Moving from 
Python', "0 :: Anyone experienced in PyQt5? Need help implementing a 'drop zone' for im...",
Help with that guys daily wallpaper script.', '1 :: Using Python 3 and Python 2 On The Same 
]  

Thanks so much guys for all your help!

Well since the workspaces are running in linux you have to check the dependencies for linux. But I'm not sure if you could install anything into the workspaces.

Therefore you could install python and praw on your local system with the suggestion that akak gave you. And then try it out.

Or contact any of the Teachers here on treehouse if you can install something additionaly to workspaces. You could try it but make sure you ask first.

Hope this was any help to you.

Happy Coding!!!

Thanks Buddy!

I have actually tried installing it locally and running it on my mac too, using PIP3 and trying both python and python3 to run it, but I get the same error.

It's not a big deal but just fancied a little experimentation, it's all good practice :)

Cheers!

@William Gough .

Well let my try it myself on my system and I'll let you know what comes out of it.

I like to experiment myself :)

If you're on mac you may want to install praw with pip3. On mac pip installs it for older version of python and might be unavailable to python3.

If you're not on mac I have no idea since the code worked for me :)

I am on a mac but I tried pip3 too and it still didn't work :( Thanks anyway buddy! I appreciate it!