1

I am trying to import the class PBase which is in base.py(its in the same folder)

So I am doing the following

from base import PBase

but I am getting the following error

Traceback (most recent call last):
  File "test_pricing.py", line 9, in <module>
    from base import PBase
ImportError: cannot import name PBase

here is my base.py

import yaml
import unittest, time, re

class PBase(unittest.TestCase):
    def enter_credentials(self, username, password):
        self.driver.find_element_by_id("id_username").clear
        self.driver.find_element_by_id("id_username").send_keys(username)
        self.driver.find_element_by_id("id_password").clear
        self.driver.find_element_by_id("id_password").send_keys(password)
        self.driver.find_element_by_css_selector("input.btn.btn-success").click()

    def get_credentials(self):
        with open('credentials.yml', 'r') as f:
            doc=yaml.load(f)
        return doc        

    def is_user_logged_in(self):
        f= "Account" in self.driver.find_element_by_css_selector(".navbar").text
        return f

    def log_in_user(self):
        self.driver.get(self.login_url)
        user_dict=self.get_credentials()
        username=user_dict["user"]["username"]
        password=user_dict["user"]["password"]
        self.enter_credentials(username, password)
        self.assertEqual(self.expected_logged_in_title, self.driver.title)

    def wait_for_chat_window(self, timeout=5):
        WebDriverWait(self.driver, timeout, poll_frequency=0.1).until(lambda b: b.find_element_by_id('habla_beta_container_do_not_rely_on_div_classes_or_names').is_displayed())
        time.sleep(3)

    def close_modal_window(self):
        driver=self.driver
        driver.find_element_by_xpath('//a[@class="close"]').click()


    def tearDown(self):
        if self.is_final_test==1:
            self.driver.quit()
6
  • 1
    Do you have a file called __init__.py in the same directory? Commented Jun 17, 2012 at 9:51
  • 2
    What happens if you just do import base? Does it work? Can you access base.PBase? Commented Jun 17, 2012 at 9:52
  • yes, i have a __init__.py in the same dir. Also, if I do import base and try to access it as base.PBase i get the following error - Traceback (most recent call last): File "test_pricing.py", line 10, in <module> class TestPricingPage(base.PBase): AttributeError: 'module' object has no attribute 'PBase' Commented Jun 17, 2012 at 9:54
  • 1
    Can you post your base module or lines where you define PBase? Commented Jun 17, 2012 at 9:58
  • strangely after i echoed base.__dict__ after importing base in the interactive python interpreter, it worked. Commented Jun 17, 2012 at 10:20

1 Answer 1

1

This might be an issue caused by importing in a circular way. For example one tries to import from Bar from bar.py that requires Foo from foo.py that requires Utils that is defined in bar.py:

foo.py:

from bar import Utils


class Foo:
    def __init__(self):
        print("hello, from Foo")
        Utils.do_something()

and bar:

from foo import Foo


class Utils:
    @staticmethod
    def do_something():
        print("done something")


class Bar(Foo):
    def __init__(self):
        print('Hi, from Bar')

This gives similar issues where one can import bar inside foo.py without any issue, but once one tries to make any reference to the class Utils inside foo.py one gets various errors, for example the previous code results in:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    from bar import Bar
  File "bar.py", line 1, in <module>
    from foo import Foo
  File "foo.py", line 1, in <module>
    from bar import Utils
ImportError: cannot import name 'Utils' from 'bar' (bar.py)

The issue is easily solved by placing Utils in its own file.

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

1 Comment

Although I am not completely certain that this is the issue, I did come by this post and found the issue almost exactly had the same issues as what i wanted to fix. But signs that this might have been the original problem is: that the original issue seems to have been part from a larger project and that WebDriverWait is used without any prior reference. This suggests the original author cut out a few import statements to the rest of the project in the code he shows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.