Hello,
I am trying run python from within Matlab. Unfortunately, I I even fail to use numpy (see below). I would very much appreciate any suggestions as to why that could be. I am using Matlab 2019b on Winows 10.
>> pyversion
version: '3.7'
executable: 'C:\Users\wolf5212\AppData\Local\Programs\Python\Python37\python.exe'
library: 'C:\Users\wolf5212\AppData\Local\Programs\Python\Python37\python37.dll'
home: 'C:\Users\wolf5212\AppData\Local\Programs\Python\Python37'
isloaded: 0
>> py.str(1)
ans =
Python str with no properties.
1.0
>> py.numpy.array([])
Unable to resolve the name py.numpy.array.
Importing numpy from the command prompt is no problem.
H:\>python
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 01:54:44) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> exit()

 채택된 답변

Igor Varfolomeev
Igor Varfolomeev 2020년 4월 9일

1 개 추천

I ran into a similar issue recently, but I'm not sure it it's 100% same.
What I can suggest is:
2. Try "InProcess" mode:
pe=pyenv('Version','C:\path\to\Python36\pythonw.exe',...
...'ExecutionMode','OutOfProcess'... --- this (new mode) causes misc. issues in R2019b update 5
'ExecutionMode','InProcess'... --- this (old mode) works OK
);
3. Try to explicitly import numpy:
pe=pyenv('Version','C:\path\to\Python36\pythonw.exe',...
'ExecutionMode','InProcess');
py.importlib.import_module('numpy')
if it produces something like this:
>> py.importlib.import_module('numpy')
Error using __init__><module> (line 54)
Python Error: ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
<...>
it is likely that Python cannot see the necessary dll files. Adding them to path fixes the issue for me.
This only happens to me when I use Anaconda python installation :
conda create -n test_py36 python=3.6 numpy
I never ran into this issue when using CPython 3.6.8.
But for Anaconda, the issue could be fixed by simply adding the folder with the neccessary dlls to %PATH%:
username = getenv('username');
setenv('PYTHONUNBUFFERED','1'); % doesn't change much, but just-in-case
setenv('path',['C:\Users\' username '\Anaconda3\envs\test_py36\Library\bin;', getenv('path')]);
pe=pyenv('Version',['C:\Users\' username '\Anaconda3\envs\test_py36\pythonw.exe'],...
'ExecutionMode','InProcess');
py.importlib.import_module('numpy')

댓글 수: 2

Paul F Lang
Paul F Lang 2020년 4월 12일
Thanks a lot Igor, I'll try that out next time I'll get back to this problem.
Thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by