开发者

python check for matlab installation

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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜