Troubleshooting Calling Python in MATLAB
If you have Python® installed on your machine, it should just load without you doing anything: Simply type “py” and then the name of the Python function you wish to call. But this is programming, so things don’t always work. Walk through the steps for checking if Python and MATLAB® are communicating on a Windows®, Linux®, or Mac® machine.
Published: 10 Nov 2022
Hello, and welcome to another MATLAB tutorial. Today, we're talking about connecting with your Python install in MATLAB. If you have Python installs on your machine, it should just load without you doing anything. Simply type py dot and then the name of the Python function you wish to call.
For example, if you want to list, just type py.list and then the contents of your list. And boom, you have a Python list. To check which Python environment type you're using and if MATLAB is connected to it, run the command py envi.
But I'm assuming here that last step didn't work. This is programming. Things don't always work. So let's run through different troubleshooting steps to take if your Python install isn't communicating with MATLAB.
First things first, we need to check and make sure the version of Python we've installed is compatible with the version of MATLAB we're working with. The MathWorks website has a handy-dandy sheet that shows all the different Python and MATLAB compatibilities. It can be found on the doc page Configure Your System to Use Python linked in the description. We're using 3.9 in MATLAB 22A, so we should be good.
The next step is to try the command py envi again, but with the location of the Python executable. Simply feed the function the argument's version in quotation marks and then the address of your Python install.
There are two ways to find the location of Python on your machine. The first is, on Windows, to open Command Prompt and enter where Python. Or on Mac and Linux machines, open the Terminal and input which Python. However, Mac machines will default to Python 2.7, which comes preinstalled. This is likely not the version of Python you're trying to use. So you'll need to clarify Python 3.
The other option is to open the Python Command window and input import OS, import sys, os.path.dirname(sys.executable). Either way should give you a file address for the executable. Copy and paste it into your MATLAB code. Format it so that its only single slashes. Make it a string. And if it doesn't end with python.exe, add it with a slash in front. Then when you run your code, it should be able to locate the Python install.
If that doesn't work, you're likely experiencing one of two issues. The first is that you may have downloaded Python from the Windows Store. If so, your address for Python is probably something along the lines of AppData slash local slash Microsoft slash Windows App slash python.exe.
While convenient, the Windows Store version of Python has a propensity to cause bugs, both when calling it from programs such as MATLAB, but also when trying to do basic Python tasks, such as pip install. Your best bet is to re-install Python from the Python Foundation's website.
If you're not using the Windows Store version, then it's likely your Python install isn't on the program path. Copy the location of your Python install. And then if you're on a Windows machine, open up Advanced System Settings and go down to Environment Variables.
Click New, paste the address of value, and name it something like python_home. Then click the path variable right here. Select Edit. Go to the end of the variable value. Add a semicolon and then the name of your Python variable-- in my case python_home. Then you just save and close out. And you should be good to go.
But what if you're on a Mac or Linux machine? Well, unfortunately, these operating systems don't have a GUI to assign path variables. So instead, you're going to need to use the Command Line interface.
Now, most Linux, and Mac OS use Bash shell, but not all. If you're using macOS Catalina or later, your machine uses Z shell. Catalina was released in 2019, if that helps you figure out which OS you're using. If you're on a Linux machine and you want to check which shell you're using, simply run the command echo $0.
Now, this tutorial is going to explain setting the path if you're using Bash shell or Z shell, as it's roughly the same thing. You just substitute Bash or Z, respectively. It may be different if you're using something that is neither Bash or Z.
I'm also presuming that you have Nano installed on machine. If you're using a Mac, it came with Nano preinstalled, so don't worry. The first step is to find your path file. In the Terminal, type, for Bash, one of the following commands, or for Z shell, one of these commands.
Once you find your Path file, insert export PATH=$PATH; and then the address of your Python install. Save the file, quit the Terminal window, and reopen the Terminal, and run echo $PATH to confirm that your changes were saved.
We've now covered some of the main reasons your Python install isn't communicating with MATLAB. If this didn't cover your issue, please explore the documentation, MATLAB Answers, or even file a help ticket with support. Thank you for watching.