0

When I run my Playwright test, I get the following error:

ImportError while importing test module 'C:\Users\fsdam\OneDrive\Documents\python_work\playwright-python-tutorial\tests\test_search.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Users\fsdam\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_search.py:2: in <module>
    from pages.search import DuckDuckGoSearchPage
E   ModuleNotFoundError: No module named 'pages'

It complains that there is no module names pages. But pages is a package. I am following a tutorial and I do not see any difference in my code and folder structure when compared. Any help would be great.

Here is my folder structure:

.
└── playwright_python_tutorial  /
    ├── pages/
    │   ├── __init__.py
    │   ├── result.py
    │   └── search.py
    └── tests/
        └── test_search.py  

Here is my code that contains the test containing the imports:

from playwright.sync_api import expect, Page
from pages.search import DuckDuckGoSearchPage
from pages.result import DuckDuckGoResultPage

def test_basic_duckduckgo_search(page: Page) -> None:
    search_page = DuckDuckGoSearchPage(page)
    result_page = DuckDuckGoResultPage(page)

    #Given the DuckDuckGo home page is displayed
    search_page.load()

    #When the user searches for a phrase
    page.locator('id=searchbox_input').fill('panda')
    #page.fill('id=searchbox_input', 'panda')
    page.locator('xpath=//button[@aria-label="Search"]').click()

    #Then the search result query is the phrase
    expect(result_page.search_input).to_have_value('panda')
    # Alternative way:  assert 'panda' == page.input_value('id=search_form_input')

    #And the search result links pertain to the phrase
    assert result_page.result_link_titles_contain_phrase('panda')

    #And the search result title contains the phrase 
    expect(page).to_have_title('panda at DuckDuckGo')
fdama
  • 194
  • 3
  • 6
  • 15

1 Answers1

-1

Solved this by adding:

import sys
sys.path.append('../') 

From here

fdama
  • 194
  • 3
  • 6
  • 15
  • This answer may be getting downvoted since using fiddling with `sys.path` is usually an unnecessary hack. It's generally preferable to have your package structure / Python path set up correctly from the start. In this case you seem to want `pages` to be a top-level package, so you should configure your environment so that `playwright_python_tutorial` is on he Python path before the interpreter even starts. That's usually done by installing your project as a distribution package or by relying on your CWD to be the top of your source directory – Brian61354270 Apr 14 '23 at 19:43
  • @Brian61354270 Thanks for your comment. Could you give me some guidance how to do this? I'm new to all this. When you say install I am used to installing external libraries, not my own. – fdama Apr 15 '23 at 18:34
  • 1
    You can check out the [official packaging guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/). Basically, just create a `pyproject.toml` file, add some configuration for your build system of choice (I'd recommend [poetry](https://python-poetry.org/)), then just `pip install -e .`. That will place your project's sources on the current interpreter's import path, making your packages importable from anywhere. – Brian61354270 Apr 15 '23 at 19:20