KeyError:从本地目录导入时发生“'__name__'不在全局变量中”

时间:2019-11-19 16:20:06

标签: 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

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

上运行行为app \ features \ steps \ login.py时,以下文件将发生错误
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

这是在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)

我正在关注此站点上的文档-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"

1 个答案:

答案 0 :(得分:-1)

重新整理其处理方式的代码

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

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