DarthOpto
DarthOpto

Reputation: 1652

Cannot Import From a Class

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.

Upvotes: 1

Views: 531

Answers (1)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143124

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.

Upvotes: 4

Related Questions