Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this Python code running twice? [duplicate]

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.

like image 664
Bruce Avatar asked Jul 08 '13 15:07

Bruce


People also ask

Why is my python program running twice?

If you see that twice it would mean the script is running twice. If that happens try running the script from the command line instead of spyder. Feb-02-2022, 10:21 AM (This post was last modified: Feb-02-2022, 10:22 AM by mcva.)

Can you run the same python file twice?

You can even run the same configuration multiple times if it has Allow parallel run enabled. In Terminal Tool Window, open two tabs and run your scripts in each; In Python Console Tool Window, again open two tabs, in each import your code and call the program.


2 Answers

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.

like image 164
Martijn Pieters Avatar answered Oct 23 '22 03:10

Martijn Pieters


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

like image 40
user2550714 Avatar answered Oct 23 '22 04:10

user2550714