Reputation: 1260
I am running a python program on a server, and on my account on the server I have the python version set in .bashrc file as follows:
alias python="python2.7"
I have a python script that I would like to be able restart itself. It works fine locally, but when I restart it on the server, it works once, and then switches to a different version of python. I have the following function:
def restartScript(self):
print("Restarting server")
print(sys.executable,['python']+sys.argv)
os.execv(sys.executable,['python']+sys.argv)
The first time I try to restart it prints the following:
/usr/local/bin/python2.7 ['python', 'server.py']
However the second time I run the server, it prints the following:
/usr/local/bin/python ['python', 'server.py']
This also gives an error, because I am using a module that is installed for /usr/local/bin/python2.7 but isn't installed for /usr/local/bin/python.
Is there an easy way to make sure that the server always restarts with /usr/local/bin/python2.7? I would like to make it flexible so that someone can use this restart whether they have defined their default version of python in .bashrc or are using a virtual environment. Also would like it work if they are using python 3 or python 2.
Upvotes: 0
Views: 764
Reputation: 1260
The following works independent of which python version you are using:
os.execv(sys.executable,[sys.executable.split("/")[-1]]+sys.argv)
Upvotes: 1
Reputation: 7187
I feel like I may be misunderstanding something, but I think you just want to pass 'python2.7'
as the first element of your list of arguments instead of 'python'
. Or if you're not sure what the executable name will be, pass sys.executable
.
Upvotes: 0