1

I have two python files First python code in file 1:

import simpleT

def funcion():
   print "Function called"
if __name__=="__main__":
  try:
     simpleT.Door().start()
     while True:
        time.sleep(1.5)
        print "Main Op"

File 2(simpleT.py)

import threading
import time
class Door(threading.Thread):   
  def __init__ (self):
     threading.Thread.__init__(self)
  def run(self):    
     funcion()

Step 1: I can execute the function if the class of thread is in the same file
Step 2: I want split it, execute "function" localted on file 1 that containts the main function from the thread located on file 2 but error say: NameError: global name "function" is not defined
how can i call this function?..is there super class or parameter required?

4
  • You have to import it, but to prevent circular imports you should put function into simpleT.py instead. This is BTW unrelated to threading. Commented Jul 21, 2019 at 5:36
  • So..basically i can't call from thread..i need to put into simpleT.py..is that right? Commented Jul 21, 2019 at 5:41
  • Is there anything my earlier comment left unclear? Commented Jul 21, 2019 at 5:47
  • As a new user, please also take the tour and read How to Ask. If you had provided a minimal reproducible example, you would also have found out that this problem is not related to threading. Commented Jul 21, 2019 at 10:07

1 Answer 1

1

You need to import the function from file 1 in simpleT.py But in that way, it will be a cyclic import and will throw an error.

So the best would be to create a new module for function

file2.py

def funcion():
    print "Function called"

Then import this function in simpleT.py

import threading
import time

from file2 import function


class Door(threading.Thread):   
    def __init__ (self):
        threading.Thread.__init__(self)
    def run(self):    
        funcion()

and then in file1.py

import simpleT


if __name__=="__main__":
    try:
        simpleT.Door().start()
        while True:
            time.sleep(1.5)
            print "Main Op"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.