14

I have a Python script with just these 2 lines:

import requests
print len(dir(requests))

It prints:

12
48

When I print the actual list dir(requests), I get this:

['__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__']
['ConnectionError', 'HTTPError', 'NullHandler', 'PreparedRequest', 'Request', 'RequestException', 'Response', 'Session', 'Timeout', 'TooManyRedirects', 'URLRequired', '__author__', '__build__', '__builtins__', '__copyright__', '__doc__', '__file__', '__license__', '__name__', '__package__', '__path__', '__title__', '__version__', 'adapters', 'api', 'auth', 'certs', 'codes', 'compat', 'cookies', 'delete', 'exceptions', 'get', 'head', 'hooks', 'logging', 'models', 'options', 'packages', 'patch', 'post', 'put', 'request', 'session', 'sessions', 'status_codes', 'structures', 'utils']

I'm guessing there are multiple requests modules or something like that. Please help.

9
  • What "actual arrays" are these? When do you print them? More information, please. Commented Jul 8, 2013 at 15:10
  • Oops. The list returned by dir(requests). Edited Commented Jul 8, 2013 at 15:12
  • But you're only doing that once. Under what circumstances do you get each? Commented Jul 8, 2013 at 15:13
  • @kindall The script has just these 2 lines Commented Jul 8, 2013 at 15:13
  • 1
    It'll probably be worth mentioning in terms of context that this question is a spin-off from attempts to sort this earlier question Commented Jul 8, 2013 at 15:14

2 Answers 2

17

You gave your script a name of a standard module or something else that is imported by the requests package. You created a circular import.

yourscript -> import requests -> [0 or more other modules] -> import yourscript -> import requests again

Because requests didn't complete importing the first time you get to see these differences in the list of supported objects.

Don't do that. Rename your script to something else and it'll all work.

Sign up to request clarification or add additional context in comments.

2 Comments

Jesus. It was http.py.
This answer saved my life. Thank you! I'm executing multiple scripts in one go and discovered that I created a circular import that caused the same issue. Lesson learned!
1

First one is your own module Second is module for dealing with HTTP requests. Rename ur own module

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.