python - Script calling multiple other scripts erroring due to undefined functions -


i'm unsure again i'm sure going simple...

basically, i'm trying make first script call/execute bunch of other scripts problem want each individual script contain own functions not called first secript...

first script/main script:

from datetime import date, timedelta sched import scheduler time import time, sleep, strftime import random  s = scheduler(time, sleep) random.seed()  def periodically(runtime, intsmall, intlarge, function):      ## current time     currenttime = strftime('%h:%m:%s')      ## if currenttime anywhere between 23:40 , 23:50 then...     if currenttime > '23:40:00' , currenttime < '23:50:00':         ## call clear         clear()         ## update time         currenttime = strftime('%h:%m:%s')      ## idle time     while currenttime > '23:40:00' , currenttime < '23:59:59' or currenttime >= '00:00:00' , currenttime < '01:30:00':         ## update time         currenttime = strftime('%h:%m:%s')      runtime += random.randrange(intsmall, intlarge)     s.enter(runtime, 1, function, ())     s.run()  def callscripts():     print "calling functions"      errors = open('error(s).txt', 'a')     try:          execfile("data/secondary.py")     except exception e:         errors.write(str(e))         errors.write(""" """)              errors.close()   while true:     periodically(2, -1, +1, callscripts) 

below secondary.py

import win32con win32api import * win32gui import *  class windowsballoontip:     def __init__(self, title, msg):         message_map = { win32con.wm_destroy: self.ondestroy,}          # register window class.         wc = wndclass()         hinst = wc.hinstance = getmodulehandle(none)         wc.lpszclassname = 'pythontaskbar'         wc.lpfnwndproc = message_map # specify wndproc.         classatom = registerclass(wc)          # create window.         style = win32con.ws_overlapped | win32con.ws_sysmenu         self.hwnd = createwindow(classatom, "taskbar", style, 0, 0, win32con.cw_usedefault, win32con.cw_usedefault, 0, 0, hinst, none)         updatewindow(self.hwnd)          # icons managment         iconpathname = "icon1.ico" ## location icon file         icon_flags = win32con.lr_loadfromfile | win32con.lr_defaultsize         try:             hicon = loadimage(hinst, iconpathname, win32con.image_icon, 0, 0, icon_flags)         except:             hicon = loadicon(0, win32con.idi_application)         flags = nif_icon | nif_message | nif_tip         nid = (self.hwnd, 0, flags, win32con.wm_user+20, hicon, 'tooltip')          # notify         shell_notifyicon(nim_add, nid)         shell_notifyicon(nim_modify, (self.hwnd, 0, nif_info, win32con.wm_user+20, hicon, 'balloon tooltip', msg, 200, title))         # self.show_balloon(title, msg)         sleep(5)          # destroy         destroywindow(self.hwnd)         classatom = unregisterclass(classatom, hinst)     def ondestroy(self, hwnd, msg, wparam, lparam):         nid = (self.hwnd, 0)         shell_notifyicon(nim_delete, nid)         postquitmessage(0) # terminate app.  # function def balloon_tip(title, msg):     w=windowsballoontip(title, msg)   balloon_tip("test test", "running")  def hi():     print "hi"  hi() 

error:

global name 'windowsballoontip' not defined 

full error:

traceback (most recent call last):   file "c:\main.py", line 48, in <module>     periodically(2, -1, +1, callscripts)   file "c:\main.py", line 27, in periodically     s.run()   file "c:\python27\lib\sched.py", line 117, in run     action(*argument)   file "main.py", line 34, in callscripts     execfile("data/secondary.py")   file "data/secondary.py", line 93, in <module>     balloon_tip("test test", "running")   file "data/secondary.py", line 78, in balloon_tip     w=windowsballoontip(title, msg) nameerror: global name 'windowsballoontip' not defined 

how go fixing this?

thanks in advance hyflex

first of all,

class windowsballoontip: 

should be

class windowsballoontip(object): 

because former old style class, has disappeared in python 3 , in recent versions of python 2.x backwards compatibility.

ethan's answer correct, unclear if you're asking question. full explanation here.

when ballon_tip() run, first searches local namespace -- balloon_tip()'s namespace -- called windowsballoontip. when can't find it, searches global namespace. since didn't provide globals parameter execfile(), defaults namespace of callscripts(), doesn't have named windowsbaloontip inside of it, , errors.

to fix this, can pass globals() argument execfile, pollute global namespace of main script, don't want. can declare inside of secondary.py global, don't want since whole point test secondary.py.

the issue execfile. execfile ugly, hack-y way things. import better. 1 solution write inside secondary.py:

def test_harness():     balloon_tip("test test", "running")     hi() 

then, import secondary, traceback inside main script, , change callscripts() this:

def callscripts():         print "calling functions"         errors = open("errors(s).txt", "a")          try:             secondary.test_harness()         except:             errors.write(traceback.format_exc() + '\n') 

edit in response comment: @ top of script, import traceback, then:

def callscripts():         print "calling functions"         errors = open("errors(s).txt", "a")          try:             execfile("data/secondary.py", {})         except:             errors.write(traceback.format_exc() + '\n') 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -