[英]Why does relocating the running exe makes it stop?
基本上我使用 PyInstaller 將這個 python 文件轉換為可執行文件。 但是從內部重新定位exe的路徑,使其停止運行。 從重新定位的路徑重新運行可執行文件不會導致任何錯誤,並且工作正常。
使用 while True 循環 main 函數使其保持運行,但無法與服務器建立任何連接。 (在 Agent 類中完成)
這是轉換為 exe 的 python 文件的一部分
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()
通過再次調用exe文件來解決它(並且只允許當前的一個關閉)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.