0

I am creating a wrapper around a few python scripts and have run into a slight issue when there are multiple versions of python installed on the computer. For example on my Mac there is python 2.7 accessible via "python" at the command line and python 3.4 available "python3". Is there anyway to determine how the current python instance was started so that I can be sure the subprocess will be using the right version?

import subprocess

def main():
    pythonCommand = determineCommand() #What is python install as on this computer
    argArray = [pythonCommand, "test.py"] #requires python 3.4
    subprocess.call(argArray)

#What I need to figure out
def determineCommand():
    #If Some Check
        return "python3"
    #Else some other check
        return "python"
    #else something weird
        return "python34"
    #and so on

if __name__ == "__main__":
    main()

The above code will not execute properly on my computer but on a computer with only python 3.4 installed it works fine. Changing the argArray to use python3 works on my computer but breaks it on others.

user1593858
  • 649
  • 7
  • 12
  • related: [Call python script with input with in a python script using subprocess](http://stackoverflow.com/q/30076185/4279) – jfs Nov 17 '15 at 04:20

2 Answers2

2

The version of Python being executed can be checked via sys.version_info:

chuck@computer:~$ python
>>> import sys; sys.version_info
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
>>> exit()

chuck@computer:~$ python3
>>> import sys; sys.version_info
sys.version_info(major=3, minor=4, micro=3, releaselevel='final', serial=0)
>>> 

The version_info attribute is available in all Python versions >= 2.0.

If you require a certain version, you can add a check in your module:

import sys
v = sys.version_info

if sys.version_info.major < 3:
    raise Exception("Incompatible Python version: %d.%d" % (v.major, v.minor))
chucksmash
  • 5,777
  • 1
  • 32
  • 41
  • Is there anyway to know that command line command that will invoke that version? Like how would i know to use python3 and not python34 on some unusual installation? – user1593858 Nov 17 '15 at 02:13
  • @user1593858 You can pass the -c flag at the command line: `python -c "import sys; print sys.version_info"` if you want to see the full version_info object or just `python --version` otherwise. – chucksmash Nov 17 '15 at 02:17
  • I guess what I am asking is unclear. I added some to the code in the question to hopefully illustrate what I am trying to ask – user1593858 Nov 17 '15 at 02:27
  • @user1593858 Thanks for the clarification. I'd say you don't want to rely on the specific name the user invokes python with; using `export p=/usr/bin/python` for instance, I can call the Python 2.7 interpreter on my system as `$p`. – chucksmash Nov 17 '15 at 02:51
  • @user1593858 More generally, I'd suggest either updating your code to be 2.7 compatible if the case requires it or in your documentation spelling out exactly which versions of Python are supported and raising an exception if an incorrect Python version is detected. If you want to support 2 and keep writing 3, check out the [future package](https://pypi.python.org/pypi/future) – chucksmash Nov 17 '15 at 02:58
  • That does sound like a much better way to go in general but for this rough prototype i just needed it to work on two or three computers and @ShadowRanger 's did the trick. Thanks for your help though, I learned something new – user1593858 Nov 17 '15 at 03:47
2

To get the executable used to launch the current Python interpreter, read sys.executable. It's the absolute path to the Python interpreter binary currently running (though it can be the empty string or None in weird cases, like frozen executables and the like).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271