python check for matlab installation

Question:

I would like to know if there is a way I could check from my python code if matlab exists on a system. So far the only thing I can come up with is: exists = os.system("matlab") and then parse the exists for a command not found. But I’m almost sure this will:

  1. Start matlab in case it exists on the system. I don’t want this.
  2. The response may vary depending on the system I’m running ?

So is there any way I could check if a matlab installation is available on the system from python ?

Regards,
Bogdan

Asked By: Bogdan

||

Answers:

Assuming your system call works, you can check the path for matlab.exe like this:

import os

def matlab_installed():
    for path in os.environ["PATH"].split(";"):
        if os.path.isfile(os.path.join(path, "matlab.exe")):
            return True
    return False

For Unix, you have to change split(“;”) to split(“:”) and “matlab.exe” to whatever the matlab executable is called under Unix.

Answered By: Jacob

Another way would be to to use shutil

import shutil
mt = shutil.which("matlab")

If ‘matlab’ is found it returns the path where it was found, else it returns ‘NoneType’. You may check for ‘matlab’ or ‘matlab.exe’ depending on the OS.

Answered By: atinjanki
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.