I have a python-based GTK application that loads several modules. It is run from the (linux) terminal like so:
./myscript.py --some-flag setting
I have just a little amelioration on the code of #s3niOr.
In my case there are spaces in the path of the python file. So by adding a proper formatting, the problem can be solved.
Notice that in my case my python file has no other arguments. So if you do have other arguments, you have to deal with them.
This solves the problem of restarting a python script that has spaces in its path :
import os
import sys
import psutil
import logging
def restart_program():
"""Restarts the current program, with file objects and descriptors
cleanup
"""
try:
p = psutil.Process(os.getpid())
for handler in p.get_open_files() + p.connections():
os.close(handler.fd)
except Exception, e:
logging.error(e)
python = sys.executable
os.execl(python, python, "\"{}\"".format(sys.argv[0]))