简体   繁体   中英

Why does relocating the running exe makes it stop?

Basically I convert this python file to an executable with PyInstaller. But relocating the path of the exe from within, makes it stop from running. Re-running the executable from that relocated path doesn't cause any errors, and works just fine.

Looping the main function using while True keeps it running, but fails to establish any connection with the server. (done in the Agent class)

This is a part of the python file that gets converted to exe

def main():

    try:
        # try to relocate file (always works)
        root_path = os.environ["HOMEPATH"]
        file_name = os.path.basename(sys.executable)

        current_path = os.path.realpath(sys.executable)[2:]

        path_needed = os.path.join(root_path, file_name)

        if current_path != path_needed:
            print(str(current_path) + " != " + str(path_needed))
            os.rename(current_path, path_needed)

    except Exception as e:
        print("main exception with exception " + str(e))


    agent = Agent()
    agent.run()


if __name__ == "__main__":
    # while True:
    main()

Solved it by calling the exe file again (and just allow the current one to close out)

if current_path != path_needed:
        print(str(current_path) + " != " + str(path_needed))
        os.rename(current_path, path_needed)
        try:
            os.startfile("C:\\" + path_needed)
        except Exception as exception:
            print("main execute exception with exception " + str(exception))                
        return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM