Hi, I need to run Selenium in my Flyte workflow o...
# flyte-deployment
t
Hi, I need to run Selenium in my Flyte workflow on Flyte. But since the selenium library is not installed, it gives the following error. Please help me.
Copy code
Trace:

    Traceback (most recent call last):
      File "/usr/local/lib/python3.10/site-packages/flytekit/bin/entrypoint.py", line 164, in _dispatch_execute
        task_def = load_task()
      File "/usr/local/lib/python3.10/site-packages/flytekit/bin/entrypoint.py", line 583, in load_task
        return resolver_obj.load_task(loader_args=resolver_args)
      File "/usr/local/lib/python3.10/site-packages/flytekit/core/utils.py", line 312, in wrapper
        return func(*args, **kwargs)
      File "/usr/local/lib/python3.10/site-packages/flytekit/core/python_auto_container.py", line 271, in load_task
        task_module = importlib.import_module(name=task_module)  # type: ignore
      File "/usr/local/lib/python3.10/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
      File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
      File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 883, in exec_module
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "/root/browser_test.py", line 2, in <module>
        from selenium import webdriver
    ModuleNotFoundError: No module named 'selenium'

Message:

    ModuleNotFoundError: No module named 'selenium'
Copy code
from flytekit import task, workflow
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time
import subprocess

@task
def open_google() -> str:
    install_selenium()
    # # # service = Service('/usr/local/bin/chromedriver')
    # # # options = webdriver.ChromeOptions()
    # # # driver = webdriver.Chrome(service=service, options=options)
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
    driver.get("<https://www.google.com>")
    time.sleep(3)
    title = driver.title
    driver.quit()
    return f"Sayfa başlığı: {title}"

@workflow
def browser_test_wf() -> str:
    return open_google()

if __name__ == "__main__":
    print(browser_test_wf())
s
Flyte tasks are executed in virtual containers, and if you don't specify an image in the @task() decorator, flyte will just use a default container image (which will only have python basic libraries). You have to use the ImageSpec class to define a custom image which has the required libraries to run your code and specify this ImageSpec object in your tasks so flyte know to use that image to run your code (if image doesn't exist, Flyte will create the image before running code)
See this page for more information on creating custom images using imagespec objects
@thousands-airline-37812 This page goes further into how flyte uses/creates images
t
Thank you for your help