Raj
Raj

Reputation: 89

How to fix NameError When I call a function from another file

I wanted to log in to Gmail using selenium and undetected_chromedriver. Successfully logged in using these codes. My gmail_login.py files codes:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver.v2 as uc

if __name__ == '__main__':

        options = uc.ChromeOptions()
        options.add_argument("--ignore-certificate-error")
        options.add_argument("--ignore-ssl-errors")
        # e.g. Chrome path in Mac =/Users/x/Library/xx/Chrome/Default/
        # options.add_argument( "--user-data-dir=<Your chrome profile>")
        driver = uc.Chrome(options=options)
        url='https://accounts.google.com/servicelogin'
        driver.get(url)
        # add email
        driver.find_element(By.XPATH, '//*[@id="identifierId"]').send_keys(gmail_uid)
        driver.find_element(By.XPATH, '//*[@id="identifierNext"]/div/button/span').click()
       
        driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(gmail_pwd)
        driver.find_element(By.XPATH, '//*[@id="passwordNext"]/div/button/span').click()
       

        def my_custom_function(url):

                get_url = driver.get(url)

                return get_url

But When I am trying to call my my_custom_function(url) from a different python file, Got NameError. My new_file.py looks like

from gmail_login import *

print(my_custom_function('https://google.com')) 

NameError: name 'my_custom_function' is not defined

I am assuming this NameError raise for this if __name__ == '__main__': indentation

How can I solve this? Both of the files are in same folder.

Upvotes: 0

Views: 89

Answers (1)

opcy
opcy

Reputation: 26

Using if __name__ == "__main__": provides an entrypoint for your project and usually used when you have multiple files and want to specify what will happen when you run the project.

For further reading: https://www.freecodecamp.org/news/if-name-main-python-example/

So all the code, variable and function (as in your case my_custom_function) declarations are run once the gmail_login.py file is executed. That's why it says it is not defined. What to do? You could define your function above if statement and then call it in if statement and import wherever you want. Hope it helps!

Shortly refactoring your code as how i would do as an example:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import undetected_chromedriver.v2 as uc

def get_driver():
        options = uc.ChromeOptions()
        options.add_argument("--ignore-certificate-error")
        options.add_argument("--ignore-ssl-errors")
        # e.g. Chrome path in Mac =/Users/x/Library/xx/Chrome/Default/
        # options.add_argument( "--user-data-dir=<Your chrome profile>")
        driver = uc.Chrome(options=options)
        url='https://accounts.google.com/servicelogin'
        driver.get(url)
        # add email
        driver.find_element(By.XPATH, '//*[@id="identifierId"]').send_keys(gmail_uid)
        driver.find_element(By.XPATH, '//*[@id="identifierNext"]/div/button/span').click()
       
        driver.find_element(By.XPATH, '//*[@id="password"]/div[1]/div/div[1]/input').send_keys(gmail_pwd)
        driver.find_element(By.XPATH, '//*[@id="passwordNext"]/div/button/span').click()

        return driver

def my_custom_function(url):
        driver = get_driver()
        get_url = driver.get(url)

        return get_url

if __name__ == "__main__":
        my_custom_function()

Upvotes: 0

Related Questions