0

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?

2
  • run this script as administartor, may help.. Commented Jan 22, 2013 at 10:39
  • Nope. And it wont have root privilages in the future. Commented Jan 22, 2013 at 10:43

2 Answers 2

2

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:])

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Well, that's the point - python doesn't have python2 symlink in OSX.
@ciembor Then I would recommend contacting the maintainer of that package and suggesting they fix that bug. The PEP is quite clear - "Unix-like software distributions (including systems like Mac OS X and Cygwin) should install the python2 command into the default path whenever a version of the Python 2 interpreter is installed, and the same for python3 and the Python 3 interpreter.".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.