Python 3 ModuleNotFoundError:没有名为“功能”的模块

时间:2019-07-15 16:43:36

标签: python selenium pycharm cucumber python-behave

我收到ModuleNotFoundError:从步骤python文件导入页面对象模型时,没有名为“功能”的模块。我正在使用python 3 MacBook。

功能/步骤/tutorial.feature

import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from behave import *


from features.lib.pages.login_page import LoginPage

use_step_matcher("re")


@given("user is lead to the login page")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    context.browser.get("https://www.phptravels.net/login")


@when("I log in")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    page = LoginPage(context)
    page.login()

功能/lib/pages/login_page.py

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec


class LoginPage:
    locator_dictionary = {
        "email_field": (By.NAME, 'username'),
        "password_field": (By.ID, 'password'),
        "login_button": (By.CSS_SELECTOR, '.btn.btn-action.btn-lg.btn-block.loginbtn'),
        "accept_cookies": (By.ID, 'cookyGotItBtn')
    }

    def __init__(self, context):
        self.browser = context.browser

    def login(self, username="xxxxx", passwd="xxxxx"):
        b = self.browser

        WebDriverWait(b, 10).until(
            ec.visibility_of_element_located((By.ID, "cookyGotItBtn"))).click()

        b.find_element_by_name('username').send_keys(username)
        b.find_element_by_id('password').send_keys(passwd)
        b.find_element_by_css_selector('.btn.btn-action.btn-lg.btn-block.loginbtn').click()

features / tutorial.feature

Feature: showing off behave

  Background: user is lead to the login page
    Given user is lead to the login page

  Scenario: login invalid
    When I log in

我的目录结构是:

path = /Users/fran/PycharmProjects/prueba1
path/features/tutorial.feature
path/features/__init__.py
path/features/lib/__init__.py
path/features/lib/pages/__init__.py
path/features/lib/pages/login_page.py
path/features/steps/__init__.py
path/features/steps/tutorial.py

出现此错误

  File "steps/tutorial.py", line 10, in <module>
    from features.lib.pages.login_page import LoginPage
ModuleNotFoundError: No module named 'features'

如果我将其导入为

from ..lib.pages.login_page import LoginPage

我知道

  File "steps/tutorial.py", line 10, in <module>
    from ..lib.pages.login_page import LoginPage
KeyError: "'__name__' not in globals"

1 个答案:

答案 0 :(得分:0)

您遇到了Python搜索路径问题。需要正确设置Python搜索路径,才能导入其他未安装的Python模块。

问题:public static void main(String[] args){ /* create an array of some size 0 to 100000 with random integers and measure the time it takes to sort the array in ascending order (where sort() is either quicksort or mergesort.) */ long a = System.currentTimeMillis(); Random rnd = new Random(System.currentTimeMillis()); Integer[] array = new Integer[rnd.nextInt(100000)]; for(int i = 0; i < array.length; i++){ array[i] = rnd.nextInt(); } sort(array); System.out.println(Arrays.toString(array)); long b = System.currentTimeMillis() - a; System.out.println("Program runtime: " + b + "ms"); } } Expected result: Quicksort average running time should be some % faster in comparison to mergesort, for large random integer arrays. Actual result: Mergesort was slightly faster in most tests (10K elements, 100K elements, 1000K elements). 不在您的Python搜索路径中(请检查:path/在Python中的列表值)。

解决方案:

  • 在运行行为
  • 之前,将{path /“(作为绝对路径)添加到sys.path环境变量中
  • 添加一个“ path / features / environemt.py”文件,该文件通过使用PYTHONPATH
  • 将“ path /”添加到Python搜索路径中