0

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.

user1763510
  • 1,070
  • 1
  • 15
  • 28
  • Possible duplicate of [Two versions of python on linux. how to make 2.7 the default](http://stackoverflow.com/questions/19256127/two-versions-of-python-on-linux-how-to-make-2-7-the-default) – Elodin Feb 21 '17 at 00:38
  • 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 (like I have above) or are using a virtual environment. – user1763510 Feb 21 '17 at 00:48

2 Answers2

1

The following works independent of which python version you are using:

os.execv(sys.executable,[sys.executable.split("/")[-1]]+sys.argv)
user1763510
  • 1,070
  • 1
  • 15
  • 28
0

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.

happydave
  • 7,127
  • 1
  • 26
  • 25
  • I would like to make it flexible so that it works if the user is using another version of python as well. If I change os.execv(sys.executable,['python']+sys.argv) to os.execv(sys.executable,[sys.executable]+sys.argv), I still get the same behavior described above. – user1763510 Feb 21 '17 at 00:37
  • Odd. I just tried locally, and changing to use sys.executable as the first element of the list worked. It's hard to see how it would even find your /usr/loca/bin/python if you never mention the python executable in execv. – happydave Feb 21 '17 at 00:53
  • 2
    It works for me locally as well. I believe this has to do with the fact that os.execv executes in a shell that doesn't use the .bashrc file. – user1763510 Feb 21 '17 at 01:22
  • That last comment is probably true. Your alias (python='python2.7') will only work locally if you've set it in your local .bashrc - There are multiple ways to solve this of course. My move would be to just use `os.execv(sys.executable,['python2.7']+sys.argv)` as that would in most cases work both locally and when executed by another user or server. – Montmons Feb 21 '17 at 12:46