Undefined variable "py" or function "py.command"

MATLAB® automatically loads Python® when you type py.command in the Command Window. If MATLAB displays this message, a failure has occurred.

Undefined variable "py" or function "py.command"

Use this page to help troubleshoot the failure.

Python Not Installed

A supported version of Python is not installed on your computer. Review Getting Started for your MATLAB version, then download and install Python from https://www.python.org/downloads.

On Microsoft® Windows®, Python downloads the 32-bit version of Python by default. If you run a 64-bit version of MATLAB, then download a 64-bit version of Python, identified by the name "Windows x86-64 MSI installer”. For more information, see Download 64-Bit Version of Python on Windows Platforms.

On Linux® and Mac systems, if you build Python from source files, then configure the build with the --enable-shared option.

64-bit/32-bit Versions of Python on Windows Platforms

You installed a 32-bit version of Python for a 64-bit version of MATLAB. You must install a 64-bit version of Python.

MATLAB Cannot Find Python

Python is in a nonstandard location. To provide the path to the Python executable, use the pyversion function. For example:

pyversion C:\Users\uname\WinPython-64bit-3..2.1\python-3..2.amd64\python.exe

On Windows systems, Python is not found in the Windows registry. If you downloaded a Python interpreter, but did not register it in the Windows registry, specify the Python location using the command:

pyversion executable

Error in User-Defined Python Module

An error occurred in the user-defined Python module. To test if your module, mymod, contains errors, type:

py.importlib.import_module('mymod')

If Python detects an error in the module, then MATLAB displays a Python error message.

Alternatively, execute the equivalent command at the Python command prompt to get the Python error message.

After you fix the error, to access the updated module, restart MATLAB, and add it to the search path.

Python Module Not on Python Search Path

If command is a valid Python command, make sure the Python module is on the Python search path. To test if module mymod is on the path, type:

py.importlib.import_module('mymod')

If Python cannot find the module, MATLAB displays a Python error message.

To add mymod, in folder modpath, to the path, type:

P = py.sys.path;
if count(P,'modpath') == 0
    insert(P,int32(0),'modpath');
end

The Python search path is associated with the Python interpreter loaded in the current session of MATLAB. You can modify the search path in MATLAB, but the modifications are not present if you run other instances of the interpreter outside of MATLAB.

Module Name Conflicts

If you call a Python module that has the same name as a module in the standard library or any 3rd-party modules installed on your system, then MATLAB might load the wrong module.

Python Tries to Execute command in Wrong Module

If command is in a user-defined module, then make sure that the module name does not conflict with modules in the Python standard library or any 3rd-party modules on your system.

See Also

Related Topics

External Websites