基本上,我使用PyInstaller将此python文件转换为可执行文件。 但是从内部重新定位exe的路径会使它停止运行。 从该重新定位的路径重新运行该可执行文件不会导致任何错误,并且可以正常工作。
使用True时循环使用主要功能可保持其运行,但无法与服务器建立任何连接。 (在Agent类中完成)
这是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()
答案 0 :(得分:0)
通过再次调用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