Python Forum
how to do setattr() from an imported module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to do setattr() from an imported module
#1
I am trying to modify an attribute of a threaded function in the main python script from a function that has been imported from an external module.

I get this error : NameError: global name 'some_thread' is not defined

My intention is to move some functions out of the maincode.py file to the subcode.py file in order to clean up the maincode.py file and use the subcode.py file as a module. I don't know what the right way is to do setattr() from the imported module to a threaded function in the mainfile.py , nor do i know if that is even a good idea.

"mainfile.py" content :

import threading
from time import sleep
from subfile import thread_mod_func


icnt = 0

def the_thread_function(arg):
  
  while getattr(some_thread, "some_thread_en") == True:
    print("this is the value of "+ str(getattr(some_thread, "some_thread_val")) )
    sleep(0.5)

# moved this function to subfile.py
#
#def thread_mod_func(mod_val):
#  setattr(some_thread, "some_thread_val" , icnt)


if __name__ == '__main__':
   
   some_thread = threading.Thread(target=the_thread_function, args=("task",))
   setattr(some_thread, "some_thread_en" , True)                  # Initialize attribute
   setattr(some_thread, "some_thread_val" , 0)                    # Initialize attribute
   some_thread.start()
   
   
   while icnt < 5 :
     icnt =icnt + 1
     print("i am the main loop talking")
     thread_mod_func(icnt)
     sleep(1)
     
   some_thread.some_thread_en= False
   some_thread.join()
     
     
"subfile.py" contents:

import threading

def thread_mod_func(mod_val):
  setattr(some_thread, "some_thread_val" , mod_val)
Reply


Messages In This Thread
how to do setattr() from an imported module - by nutron - Sep-19-2019, 10:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 716 Aug-06-2023, 01:09 AM
Last Post: aupres
  Can a module tell where it is being imported from? stevendaprano 3 1,174 Apr-12-2022, 12:46 AM
Last Post: stevendaprano
  module detecting if imported vs not Skaperen 1 1,670 Nov-19-2021, 07:43 AM
Last Post: Yoriz
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,380 Apr-13-2021, 07:22 PM
Last Post: HeRo
  [newbie] Why is a module imported twice? Winfried 3 4,076 Apr-02-2021, 04:48 AM
Last Post: deanhystad
  argparse and imported modules spatialdawn 2 5,449 Dec-01-2018, 12:35 PM
Last Post: spatialdawn
  passing a value to imported code Skaperen 0 1,983 Sep-28-2018, 03:59 AM
Last Post: Skaperen
  function NOT imported from a module Skaperen 10 6,046 Aug-31-2018, 07:30 AM
Last Post: Gribouillis
  decorate an imported function? Skaperen 3 9,927 May-22-2017, 08:05 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020