1

I am trying to restart my main.py after the program has finished running. However, when I go into the last file (unlock.py) then put at the end of the script:

from main import *

main()

I get a circular import error. I am not sure a way to work around this so if anyone knows, your help would be greatly appreciated. If you have any questions, feel free to ask.

Eden
  • 31
  • 1
  • 10

2 Answers2

1

You can use the execv method of os module:

import os
import sys

os.execv(__file__, sys.argv)

If you're getting any permission errors:

os.execv(sys.executable, 
         [sys.executable, os.path.join(sys.path[0], __file__)] + sys.argv[1:])

To disable warnings:

import warnings
warnings.filterwarnings("ignore")
Yogesh Aggarwal
  • 1,071
  • 2
  • 12
  • 30
0

Without seeing the structure of your program, the best recommendation I can give is to pass your main function around as an argument. That way, in unlock.py, you don't need to import the main module.

Consider this simple example:

main.py

import unlock

def main(function_to_pass):
    # do stuff
    unlock.some_func(function_to_pass,*rest_of_args)

if __name__ == '__main__':
    main(main)

unlock.py

def some_func(function_to_call,*args):
   # do stuff
   if some_condition:
        function_to_call()

EDIT: I realized that you don't need to pass main into itself. main can simply reference itself. That is,

def main():
    # do stuff
    unlock.some_func(main,*args)
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45