0
Hello ,how can i run module again quickly in python?
4 Respuestas
+ 4
I don't recommend to import modules only to run them. I'd recommend to put the wanted code in functions of the module and you can run functions as many times as you want.
+ 2
If you especially want just to run the module again like it was, then I think you would be interested in compile or exec functions, I don't yet know how compile function works, but exec is very easy to use, you only put Python code (as a string) as exec's first argument:
exec("print(\"Hello world!\")") #Prints Hello world!
So you could use filehandling:
with open("module_name.py") as f:
exec(f.read())
But I discourage this method because it treats Python files as text files.
+ 1
NoreddiNe ZaOui Can you provide a brief description of what your module does and how it's being loaded?
Like Seb TheS said, you want to avoid having your module procedurally run code when it's being imported. Rather, your module should expose functions that you can call as many times as needed by the script file that imported the module.
+ 1
There is also a way that you use os.system function, which takes a string that will be used as a command for the command prompt.
os.system("echo Hello world!") #Prints Hello world!
You would need to use it to make command prompt to run the wanted Python file.
This might be more complicated.