How do you loop import module for .py files Python?

Viewed 77

I have a program that asks the user for their intent. They can decide to either Upload, Retrieve, Reset, or Set Preferences. The problem is when they upload information I need two py files to run in a loop until the requirement is met. What I have looks like sorta like this:

def MainTerminal():
    import User_Intent
    import ProceduresTerminal

    determine_intent = open(gvar.procedure_upload, 'r')
    if determine_intent.read() == '1':
        determine_intent.close()
        import Upload_Data
        import Data_Processing
 
        R_Check = open(gvar.RActivation, 'r')
        R_Check_Read = R_Check.read()
        R_Check.close()

        if R_Check_Read == '1':
            import C_Processing
            while R_Check_Read == '1':
                import R_Processing
                import C_Processing
        else:
            import C_Processing

The if functions work however, the while loop does not function. Is there a different way to do this? In the While loop C_Processing is supposed to end the loop when it sets R_Check_Read to '0', this part is working but the while loop does not.

1 Answers

Is there a different way to do this?

Yes.

Put all your import statements at top-of-file.

They should def some functions. Call a pair of such functions within your while loop.


Importing a module should mostly just define functions, plus maybe a few variables. Avoid print() statements or other side effects that happen at import time. Defer such actions until run time, when caller invokes your function.

Understand that import results are cached, so repeated import of same module will have no effect.


Use isort to manage your imports. (Their motto? "I sort your imports, so you don't have to.")

Related