A preserved archive of the Logical Gamers community forums, 2009-2025. The original threads and posts, served read-only. Registration, posting and private messages are gone for good.

[Python] Anyone feel like helping me?

1.1k views · started by 323 ·
#1
[Python] Anyone feel like helping me?
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.
#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.
#3
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.


So do I just put it in the same directory or something?

Here's my code, btw:

# 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)
#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.
#5
Tree wrote:
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.


Hm, I guess I'll have to look into pip. I was looking at their website the other day, it says something about having to run it under cygwin? Is it a *nix-only type thing?
#6
No, you need to read some documentation if this is giving you trouble.