【问题标题】:python import error, import class from another filepython导入错误,从另一个文件导入类
【发布时间】:2012-06-17 09:49:57
【问题描述】:

我正在尝试导入 base.py 中的类 PBase(它在同一个文件夹中)

所以我正在做以下事情

from base import PBase

但我收到以下错误

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

这是我的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()

【问题讨论】:

  • 在同一个目录下是否有一个名为__init__.py的文件?
  • 如果你只做import base会发生什么?它有效吗?可以访问base.PBase吗?
  • 是的,我在同一个目录中有一个__init__.py。另外,如果我导入 base 并尝试以 base.PBase 的形式访问它,我会收到以下错误 - Traceback (most recent call last): File "test_pricing.py", line 10, in &lt;module&gt; class TestPricingPage(base.PBase): AttributeError: 'module' object has no attribute 'PBase'
  • 你能在你定义 PBase 的地方发布你的基本模块或行吗?
  • 奇怪的是,在交互式 python 解释器中导入 base 后,我回显了 base.__dict__,它起作用了。

标签: python class import


【解决方案1】:

这可能是循环导入导致的问题。例如,尝试从需要从 bar.py 导入 Foo 从需要在 bar.py 中定义的 Utils 的 foo.py 的 Bar:

foo.py:

from bar import Utils


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

和酒吧:

from foo import Foo


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


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

这给出了类似的问题,可以在 foo.py 中导入 bar 而没有任何问题,但是一旦尝试对 foo.py 中的类 Utils 进行任何引用,就会出现各种错误,例如前面的代码导致:

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)

通过将 Utils 放在它自己的文件中,这个问题很容易解决。

【讨论】:

  • 虽然我不完全确定这是问题所在,但我确实来到了这篇文章,发现这个问题几乎与我想要解决的问题完全相同。但是,这可能是原始问题的迹象是:原始问题似乎来自一个更大的项目,并且 WebDriverWait 的使用没有任何事先参考。这表明原作者在他展示的代码中为项目的其余部分删去了一些导入语句。
猜你喜欢
  • 2019-02-21
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 2017-08-03
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多