Problem with installing statsmodels.api for the python supported by Matlab 2026b
Show older comments
I want to use Python's numpy, pandas, and statsmodels.api in
Matlab 2026b. I added Python using Matlab's Add-ons; i.e., I installed
MATLAB Support for Python 3.13. Then, I tried the following:
e = pyenv;
e.Version
e.Executable
e.Status
e.Version gave "3.13"; e.Executable gave
"C:\ProgramData\MATLAB\SupportPackages\R2026b\3P.instrset\python313.instrset\win64\python313\bin\pythonw.exe", and e.Status gave "Loaded".
Then, I opened a terminal and found the directory of the executable pythonw, and typed there
"python -m pip install numpy pandas statsmodels".
I could install numpy and pandas, but not statsmodels.api.
In my Matlab code, the line with
sm = py.importlib.import_module('statsmodels.api');
gives the error message
Python Error: ModuleNotFoundError: No module named 'statsmodels'.
How can I install statsmodels.api?
1 Comment
Torsten
3 minutes ago
Answer from AI:
The statsmodels module failed to import because its installation failed in your terminal. statsmodels strictly requires pre-requisites like scipy and patsy. If the automatic installation of these dependencies breaks or hits a conflict in the terminal, the package will remain unavailable.
Because you are using the MATLAB-managed Python environment, you must target the exact executable path to prevent your system's global Python from intercepting the command.
Solution 1: Install Directly from MATLAB (Recommended)
You do not need to use an external terminal. You can use MATLAB's pyrun to trigger pip inside the active Python instance. This guarantees that packages land in the correct directory.
Run these lines in your MATLAB Command Window:
matlab
% 1. Upgrade pip to ensure compatibility with Python 3.13 packages
pyrun("import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip'])");
% 2. Install statsmodels along with its core dependencies explicitly
pyrun("import sys, subprocess; subprocess.run([sys.executable, '-m', 'pip', 'install', 'scipy', 'patsy', 'statsmodels'])");
Solution 2: Fix via Windows Terminal using Absolute Paths
If you prefer using the Windows Command Prompt (cmd), you should bypass the generic python keyword and use the exact, absolute path pointing to the MATLAB target folder.
- Open your Windows Command Prompt (cmd).
- Run the following commands (note that we swap pythonw.exe for python.exe to see the terminal output):
cmd
"C:\ProgramData\MATLAB\SupportPackages\R2026b\3P.instrset\python313.instrset\win64\python313\bin\python.exe" -m pip install --upgrade pip
"C:\ProgramData\MATLAB\SupportPackages\R2026b\3P.instrset\python313.instrset\win64\python313\bin\python.exe" -m pip install scipy patsy statsmodels
Note: Because Python 3.13 is relatively recent, if pip tries to compile statsmodels from source and fails due to missing C++ compilers, append --only-binary=:all: to force a pre-compiled installation:
... -m pip install --only-binary=:all: statsmodels
Testing Your Setup
Once the installation finishes without errors, restart MATLAB completely to refresh the Python environment state. Then run:
matlab
% Import the API module
sm = py.importlib.import_module('statsmodels.api');
% Verify successful load by checking the version string
sm.version.version
If the installation fails again during the pyrun or terminal process, contact support:
Answers (0)
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!