Home > Back-end >  python call module other script
python call module other script

Time:10-23

I want to make a module script like module.py and I want to call these modules in other script files. How can I do that?

module.py

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    import os
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import TimeoutException


    opt=Options()
    opt.add_experimental_option("excludeSwitches", ["enable-automation"])


    driver = webdriver.Chrome(options=opt)

    insta = 'https://www.instagram.com/'

    driver.get(insta)

    delay = 3

and main.py

import module


........ my code

CodePudding user response:

from module import <thing to import>

You mean like this?

You can do

from module import opt, driver, insta, delay

And anything else you need.

If you want to import everything do from module import *

CodePudding user response:

One way to do it is to create a function in module.py:

# module.py
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import os
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


def start_instagram():
    opt=Options()
    opt.add_experimental_option("excludeSwitches", ["enable-automation"])


    driver = webdriver.Chrome(options=opt)

    insta = 'https://www.instagram.com/'

    driver.get(insta)

    delay = 3

Then in main.py, call that function:

# main.py
import module

module.start_instagram()
  • Related