Getting error for datetime in python
Question: I have a file named global.py and a function to create report :

    
    
    import datetime
    
    class customFail(Exception):pass
    
    def createReport(myModule,iOSDevice,iOSVersion):
        now=datetime.datetime.now()
        resultPath="../Results"
        resultFile="Result_%d_%d_%d_%d_%d_%d.html" % (now.day,now.month,now.year,now.hour,now.minute,now.second)
        fileName="%s/%s" % (resultPath,resultFile)
    
        fNameObj=open("../Results/resfileName.txt","w")     #Writing result filename temporary 
        fNameObj.write(fileName)                            #in a file to access this filename by other functions (rePass,resFail)
    
        fileObj=open(fileName,"w")
        fileObj.write("<html>")
        fileObj.write("<body bgcolor=\"Azure\">")
        fileObj.write("<p>                                                           </p>")
        fileObj.write("<table width=\"600\" border=\"5\">");
        fileObj.write("<tr style=\"background-color:LemonChiffon;\">")
        fileObj.write("<td width=\"40%\"><b>Module : </b>"+ myModule+"</td>")
        fileObj.write("<td width=\"30%\"><b>Time : </b>"+ now.strftime("%d-%m-%Y %H:%M")+"</td>")
        fileObj.write("</tr>")
        fileObj.write("<tr>")
        fileObj.write("</tr>")
        fileObj.write("</table>")
        fileObj.write("<table width=\"600\" border=\"5\">");
        fileObj.write("<tr style=\"background-color:BurlyWood;\">")
        fileObj.write("<td width=\"70%\"><b>Device : </b>"+ iOSDevice+" - <b>  Version : </b>"+ iOSVersion+"</td>")
        fileObj.write("</tr>")
        fileObj.write("</table>")
        #fileObj.write("<br>")
    

and a script file where i call this function called scripts.py

    
    
    import os
    from selenium import webdriver
    from selenium.webdriver.firefox.webdriver import WebDriver
    from selenium.webdriver.common.action_chains import ActionChains
    import time
    import sys
    sys.path.append('/Users/admin/Desktop/_Suite/Global Scripts/')
    from funcLib import *
    from myGlobal import *
    
    
    
    wd = deviceSelection();
    iOSVersion="i7"
    iOSDevice="iPhone"
    modName="BAT"
    suiteStartTime=0
    
    def main():
       start() 
       fntesttrial();
       finish(); 
    
    def  start():
           global modName,suiteStartTime
           global appName,ctx_app,ctx_simulator
           suiteStartTime=time.time();
           createReport(modName,iOSDevice,iOSVersion)
    
    stts=api_clr_acnt.fnClearAccount(myDict["UserName"],myDict["Password"],myDict["Environment"])
    
    def fntesttrial():    
        try:
        wd.find_element_by_name("Accept").click()
        time.sleep(5)   
        wd.find_element_by_name("Sign In").click()
        time.sleep(5)
        wd.find_element_by_name("Need help?").click()
        time.sleep(5)
        wd.find_element_by_name("Close").click()
        time.sleep(5)
        finally:
            wd.quit()
    
    main()
    

When I run this i am getting error like :

    
    
       now=datetime.datetime.now()
    NameError: global name 'datetime' is not defined
    

I am not understanding why I am getting that error. Please help me since i am
new to python.


Answer: **I think you need to import datetime at the top of the script file (code
block 2).** It gives you the error because datetime is indeed undefined in the
script, as it hasn't been imported yet. When you call "createReport()", "now"
can't be defined, as it calls on the datetime module, which isn't imported.

If you wanted, you could write import datetime at the start of the method
definition, but if you called the method twice, it would import datetime
twice, so you're probably better off just importing it at the start of the
second codeblock.

