So, I have a problem.
I wrote a Reddit login in Python using the Reddit API, (I was following a tutorial) but I have a problem:
It requires two external modules or libraries or whatever you call them in python.
Requests: HTTP for Humans — Requests 1.2.0 documentation
and
8.18. pprint ? Data pretty printer — Python v2.7.4 documentation
So, my problem: I have absolutely no idea how to add these so that my .py file can use them.
Anyone wanna help?
Thanks.
Results 1 to 6 of 6
- 08 Apr. 2013 08:52pm #1
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 3.35
[Python] Anyone feel like helping me?
- 08 Apr. 2013 09:01pm #2
Just import the module in a directory the script can access it from?
Just look at examples of usage. But importing a module is pretty much one of the most trivial aspects of Python. Import, initialize (if required), use. Rinse, repeat.
- 08 Apr. 2013 09:04pm #3
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 5.42
So do I just put it in the same directory or something?
Here's my code, btw:
Code:# Importing all the modules from pprint import pprint import requests import json # Set username and password username = 'USERNAMEHERE' password = 'PASSWORDHERE' # Create dict with username and password user_pass_dict = {'user': username, 'passwd': password, 'api_type': 'json'} # Set the header for all the following requests headers = {'user-agent': 'SuperFakeHeaders v1.0', } # Create a requests.session that will handle our cookies for us client = requests.session() client.headers=headers # Make a login request, passing in the user and password as data r = client.post(r'http://www.reddit.com/api/login', data=user_pass_dict) # Print to confirm error-free response pprint(r.content) # Turns the response's JSON to a native python dict j = json.loads(r.content) # Grabs the modhash from the response client.modhash = j['json']['data']['modhash'] # Prints the user's modhash print '{USER}\'s modhash is: {mh}'.format(USER=username, mh=client.modhash)
- 08 Apr. 2013 09:14pm #4
Requests isn't apart of the default Python imports. The easiest way to install it is to download and install pip, and run pip install requests.
Then you just import requests, and it should work.
You do not put it in your directory, it will be called like a normal import, you just have to install through pip, or run it through it's setup.py.
- 09 Apr. 2013 04:44pm #5
- Join Date
- Apr. 2010
- Location
- When freedom is outlawed only outlaws will be free
- Posts
- 5,113
- Reputation
- 195
- LCash
- 1.10
- 09 Apr. 2013 05:57pm #6
No, you need to read some documentation if this is giving you trouble.