Pergunta

I try to check if current version is 3 and if so, switch to python2:

#!/usr/bin/python

import sys, os

if sys.version_info[0] != 2:
    os.execl("/usr/bin/", "python2", *sys.argv)

print(sys.version_info[:])

But this script returns this error:

Traceback (most recent call last):
  File "./a.py", line 6, in <module>
    os.execl("/usr/bin/", "python2", *sys.argv)
  File "/usr/lib/python3.3/os.py", line 531, in execl
    execv(file, args)
PermissionError: [Errno 13] Permission denied

What have I missed?

Foi útil?

Solução

os.execl("/usr/bin/", "python2", *sys.argv)

/usr/bin/ is a directory, you can't run it. Try:

os.execl("/usr/bin/python2", "/usr/bin/python2", *sys.argv[1:])

Outras dicas

I would argue what you are attempting is a bad idea - it is surprising behaviour and not needed, instead, simply use an explicit hashbang:

#!/usr/bin/python2

Or, preferably:

#!/usr/bin/env python2

As per PEP 394, any unix system should provide python2.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top