为什么重定位 运行 exe 会使其停止?
Why does relocating the running exe makes it stop?
基本上,我使用 PyInstaller 将此 python 文件转换为可执行文件。
但是从内部重新定位 exe 的路径,使其从 运行 停止。
重新运行来自该重定位路径的可执行文件不会导致任何错误,并且工作正常。
循环使用 main 函数 while True 保留它 运行,但无法与服务器建立任何连接。 (在代理中完成class)
这是 python 文件的一部分,已转换为 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()
通过再次调用 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
基本上,我使用 PyInstaller 将此 python 文件转换为可执行文件。 但是从内部重新定位 exe 的路径,使其从 运行 停止。 重新运行来自该重定位路径的可执行文件不会导致任何错误,并且工作正常。
循环使用 main 函数 while True 保留它 运行,但无法与服务器建立任何连接。 (在代理中完成class)
这是 python 文件的一部分,已转换为 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()
通过再次调用 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