1

I have the following class and function. When I try to run through it I am getting:

ImportError: cannot import name Requests

Here's my code:

from tests.global_functions.util_helper import util_get_random_customer_individual
from tests.global_functions.util_helper import util_get_random_customer_company
from requests import Requests
import random


class Customer():
    def __init__(self):
        request = Requests()
        customer = None
        if request.request_type == 'individual':
            customer = util_get_random_customer_individual()
        elif request.request_type == 'company':
            customer = util_get_random_customer_company()
        else:
            print 'What the hell should I do???? HELP!?!?!?!'

The traceback is as follows:

Traceback (most recent call last):
  File "C:/Users/e003048/QA/trunk/automation/selenium/src/webservices/add_customers/webservice_requests.py", line 2, in <module>
    import webservices.system_environment.responses
  File "C:\Users\e003048\QA\trunk\automation\selenium\src\webservices\system_environment\responses.py", line 2, in <module>
    import connector
  File "C:\Users\e003048\QA\trunk\automation\selenium\src\webservices\system_environment\connector.py", line 3, in <module>
    import requests
  File "C:\Users\e003048\QA\trunk\automation\selenium\src\webservices\system_environment\requests.py", line 3, in <module>
    from customer import Customer
  File "C:\Users\e003048\QA\trunk\automation\selenium\src\webservices\system_environment\customer.py", line 4, in <module>
    from requests import Requests
ImportError: cannot import name Requests

Not sure what I am doing wrong here or why I would be getting this error. I am using PyCharm and there is nothing indicating that anything is wrong in the import statement.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
DarthOpto
  • 1,640
  • 7
  • 32
  • 59
  • Provided you don't have different PYTHONPATHs that are interfering, this is probably a name conflict, though possibly also a circular import problem. The call stack would seem to support this. Do you have any of your own packages or modules named "requests"? – Silas Ray Feb 27 '14 at 22:22
  • @SilasRay Yes, I have a `requests` module with a `Requests` class, which is what I am trying to import – DarthOpto Feb 27 '14 at 22:25
  • i would recommend trying `from request import *` though i am not sure if this is recommended –  Feb 27 '14 at 22:25
  • 1
    Oh, you aren't using the `requests` library then? In any case, it looks like your `requests` module is attempting to import the `customer` module, and the `customer` module is attempting to then import the `requests` module. This is a circular import. `Requests` doesn't exist within the `requests` namespace yet, because the module hasn't been fully loaded before `customer` attempts to load it. – Silas Ray Feb 27 '14 at 22:28
  • possible duplicate of [Circular import dependency in Python](http://stackoverflow.com/questions/1556387/circular-import-dependency-in-python) – Silas Ray Feb 27 '14 at 22:30
  • @KapelNick `import *` is never recommended. And besides, it won't help in a circular import problem anyway, as at the time of import, the object in question does not exist to be imported in the first place. – Silas Ray Feb 27 '14 at 22:32
  • @SilasRay yeah i thought so too, its not advisable to import namespace as a whole, but sometimes it might be a legit error deducting practice. –  Feb 27 '14 at 22:46
  • 1
    Given your other questions on SO about this same batch of code, I think you need a major refactor. It looks like you are basically using the `Requests` class like a glorified dictionary, while you are stuffing what should be methods on `Requests`, or subclasses thereof, in to the `util_helper` module. There appear to be fundamental structural problems with your code; addressing the narrow problem here with a hacky patch is not really going to fix your code. – Silas Ray Feb 27 '14 at 22:55

1 Answers1

4

You have circular imports. requests imports customer, and customer imports requests.

Circular imports are actually allowed in Python, but they don't work well if you're attempting to do named imports (ie from foo import Foo rather than import foo). Python is trying to import requests, but to do that it needs to import customer. So part way through importing requests it starts importing customer. Then customer says it wants to look at the thing named Request in requests, but requests isn't finished loading so that name doesn't exist yet.

There are a few possible fixes:

  1. The simplest may be for you to just switch to non-named imports. So then you'll have to say requests.Request instead of Request in the customer module.

  2. A better option would be to try to eliminate the circular dependency. In general, circular dependencies are an indication that things are too tightly coupled. Either pull the circularity out into a third module, or merge the two modules.

  3. A third option, but this is generally considered to be poor style, is to move the import of customer inside requests below the definition of the Request class. Again, this is poor style, and I do not recommend it. I mention it here only for the sake of completeness, but if you do this you will almost certainly regret it later.

Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299