KeyError:从本地目录导入时发生“'__name__' not in globals”

6 python python-behave

目录结构:

app\features\login.feature
app\features\__init__.py
app\features\steps\__init__.py
app\features\steps\login.py
app\fixtures\__init__.py
app\fixtures\fixtures.py
app\models\__init__.py
app\models\login.py
Run Code Online (Sandbox Code Playgroud)

这是我的文件里面的内容。当我在from ...models.login import Login

from behave import *
from ...models.login import Login
from ...models.footer import Footer

@given("unauthenticated user loads the home page")
def step_impl(context):
    assert Login.is_sign_in_submit_displayed(context, Login.signinbutton)

@step("sign-in button is clicked")
def step_impl(context, signinbutton):
    Login.tap_sign_in(context, Login.signinbutton)

@when("login form is populated with valid credentials")
def step_impl(context):
    Login.enter_email(context, Login.email, DEFAULT_EMAIL)
    Login.enter_password(context, Login.password, DEFAULT_PASSWORD)

@then("login is successful")
def step_impl(context):
    assert Login.home_page.is_rewards_displayed(context, Footer.menubutton)
    assert Login.home_page.is_account_displayed(context, Footer.accountbutton
Run Code Online (Sandbox Code Playgroud)

这是它在 app\models\login.py 中找不到的文件:

class Login():
    """Login screen object definitions"""
    signinbutton = ".//*[@text='SIGN IN']"
    email = ".//*[contains(@class, 'EditText')]"
    password = "(.//*[@class='android.widget.EditText'])[2]"

    def is_sign_in_submit_displayed(self, signinbutton):
        """Is the 'SIGN IN' button in the 'Login Screen' displayed?"""
        self.browser.implicitly_wait(30)
        return self.browser.find_element_by_xpath(signinbutton)

    def enter_email(self, emailelement, email):
        """Enter text into 'Email' Field in the 'Login Screen'"""
        self.browser.find_element_by_xpath(emailelement).send_keys(email)

    def enter_password(self, passelement, password):
        """Enter text into 'Password' Field in the 'Login Screen"""
        self.browser.find_element_by_xpath(passelement).send_keys(password)

    def click_sign_in_submit(self, signinbutton, menubutton, accountButton):
        """Click Sign in button and await Home Screen Nav Footer showing 'ACCOUNT' Button appearing"""
        self.browser.tap(signinbutton)
        self.browser.find_element_by_xpath(menubutton)
        self.browser.find_element_by_xpath(accountButton)
Run Code Online (Sandbox Code Playgroud)

我正在关注本网站上的文档 - https://docs.python.org/2.5/whatsnew/pep-328.html

但是当我运行这个应用程序时,它返回错误 "KeyError: "'__name__' not in globals"

我搜索了 SO 线程,但似乎没有一个解决这个问题。我的 IDE Pycharm 似乎认为导入没问题,如果我稍微更改它,它会将其突出显示为错误。

完整的堆栈跟踪:

MacBook-Pro:app jasonlegako$ behave
Exception KeyError: "'__name__' not in globals"
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/bin/behave", line 10, in <module>
    sys.exit(main())
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/__main__.py", line 183, in main
    return run_behave(config)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/__main__.py", line 127, in run_behave
    failed = runner.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 804, in run
    return self.run_with_paths()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 809, in run_with_paths
    self.load_step_definitions()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner.py", line 796, in load_step_definitions
    load_step_modules(step_paths)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner_util.py", line 412, in load_step_modules
    exec_file(os.path.join(path, name), step_module_globals)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/behave/runner_util.py", line 386, in exec_file
    exec(code, globals_, locals_)
  File "features/steps/login.py", line 2, in <module>
    from ...app.models.login import Login
KeyError: "'__name__' not in globals"
Run Code Online (Sandbox Code Playgroud)

Art*_*fin 6

重新组织它处理的代码

from ...models.login import Login
from ...models.footer import Footer
Run Code Online (Sandbox Code Playgroud)

from models.login import Login
from models.footer import Footer
Run Code Online (Sandbox Code Playgroud)