1

I have two python files and I am import one into another with:

 in test1.py
 import test2




  test2.py

   import os
   from socket import *
   host = "192.168.1.253" # set to IP address of target computer
   port = 8081
   addr = (host, port)
   UDPSock = socket(AF_INET, SOCK_DGRAM)
   data = "102"
   UDPSock.sendto(data, addr)
   UDPSock.close()

Is there any way to import test2.py with delay time in seconds?

Thanks

3
  • 7
    What do you want to achieve here? Commented Sep 29, 2018 at 17:17
  • 11
    ...why would you want to have a delay? What problem are you trying to solve? Commented Sep 29, 2018 at 17:17
  • 6
    Please read about the XY problem. Ask about the thing you want to achieve, not the solution you think is right. Commented Sep 29, 2018 at 17:19

2 Answers 2

2

While it is possible to delay an import simply by putting slower stuff above it in your file, it's probably not a good solution to your real problem, which is that you want the behavior that is triggered by the import to be delayed.

A better way to do that is to put that behavior in a function, which you can then call at any time after doing the import. The import is no longer directly triggering the behavior, so you can leave the import statement at the top of the file where most people will expect it to be.

Here are some more simplified examples.

A version based on your current code:

library.py

print("doing stuff")

script.py

import library # oops, this triggers the print too soon

An alternative script with a delay before it imports the library (which is what you're specifically asking for, though not really a great idea):

script.py:

import time

time.sleep(10)  # this does the delaying

import library  # this triggers the print, but it's unexpected to see an import here

A better solution is to move the behavior of the library into a function, so the time you do the import no longer matters (only the time you call the function):

library.py:

def stuff():
    print("doing stuff")

script.py:

import time

import library  # we can do the import whenever we want, doesn't cause any printing

time.sleep(10)
library.stuff() # do the stuff here

If you wanted the library module to also be usable as a script, you could add this code to the bottom of the file, so that it calls the function itself some times:

if __name__ == "__main__":
    stuff()

The if __name__ == "__main__": line is a bit of boilerplate that checks if the current module is the __main__ module of the program (which is true if it's being run as a script). If instead the module is being imported, that condition will be false, and so the function won't get run (until the real script decides to call it).

Sign up to request clarification or add additional context in comments.

Comments

1
import time

time.sleep(9) #delay for 9 seconds

import test2

hope this helps :)

2 Comments

I am downvoting because this is definitely an XY problem and that answer will probably end up being the right answer to the wrong problem.
@Chris Provided they have already two upvotes, the reputation penalty is fairly small. Furthermore, I'm giving a clear explanation of why I am downvoting, I think it is important for a new contributor to learn about XY problems. Finally, if that turns out to be, for some reason, the correct answer I will retract my downvote. It is ok to be downvoted, their is no shame in that.

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.