我想从Matlab (在Windows7机器上)执行Python脚本。所需的库安装在Anaconda虚拟环境中。当从命令行运行脚本时,它可以完美地运行。
从Matlab中调用脚本时,如下所示:[status, commandOut] = system('C:/Users/user/AppData/Local/Continuum/anaconda3/envs/tf/python.exe test.py');
或者使用shell命令,我得到了一个导入错误:
commandOut =
    'Traceback (most recent call last):
       File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
         from . import multiarray
     ImportError: DLL load failed: The specified path is invalid.
     During handling of the above exception, another exception occurred:
     Traceback (most recent call last):
       File "test.py", line 2, in <module>
         import numpy as np
       File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\numpy\__init__.py", line 142, in <module>
         from . import add_newdocs
       File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
         from numpy.lib import add_newdoc
       File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
         from .type_check import *
       File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
         import numpy.core.numeric as _nx
       File "C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
         raise ImportError(msg)
     ImportError: 
     Importing the multiarray numpy extension module failed.  Most
     likely you are trying to import a failed build of numpy.
     If you're working with a numpy git repo, try `git clean -xdf` (removes all
     files not under version control).  Otherwise reinstall numpy.
     Original error was: DLL load failed: The specified path is invalid.我已经将默认的Matlab Python版本更改为Anaconda env,但没有更改:
   version: '3.5'
executable: 'C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\python.exe'
   library: 'C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf\python35.dll'
      home: 'C:\Users\user\AppData\Local\Continuum\anaconda3\envs\tf'
  isloaded: 1只运行我的测试脚本而不导入numpy works。重新加载numpy (py.importlib.import_module('numpy');)不起作用,但抛出了和以前一样的错误。
有谁知道怎么解决这个问题吗?
发布于 2020-04-13 05:45:01
首先,如果您像执行常规系统命令([status, commandOut] = system('...python.exe test.py'))一样执行Python脚本,那么pyversion (和pyenv,因为是R2019b)根本不起作用。只有在使用py.集成时才重要,如下面的代码所示(在大多数情况下,这是一种更好的方法)。
目前(我使用的是R2019b update 5)有许多陷阱,可能会导致类似您的问题。我建议从以下几点开始:
conda create -n test_py36 python=3.6 numpydemo1.pydef dummy_py_method(x):
    return x+1run_py_code.mfunction  run_py_code()
% explicit module import sometimes show more detailed error messages
py.importlib.import_module('numpy');
% to reloads if there would be any changes:
pymodule = py.importlib.import_module('demo1');
py.importlib.reload(pymodule);
% passing data back and forth
x = rand([3 3]);
x_np = py.numpy.array(x);
y_np=pymodule.dummy_py_method(x_np);
y = double(y_np);
disp(y-x);before_first_run.msetenv('PYTHONUNBUFFERED','1');
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'...
         ); 
% add "demo1.py" to path
py_file_path = 'W:\tests\Matlab\python_demos\call_pycode\pycode';
if count(py.sys.path,py_file_path) == 0
    insert(py.sys.path,int32(0),py_file_path);
endbefore_first_run.m,然后运行run_py_code.m。备注:
%PATH%中。这可以通过Matlab语言中的setenv实现。通常,应该添加Library\bin。python -m pip install numpy)。根据我的经验,在这种情况下setenv是不必要的。OutOfProcess模式被证明是错误的。因此,我建议显式设置InProcess模式(对于R2019b之前的版本,OutOfProcess选项不存在,并且pyenv)..m文件连接成一个- py.importlib语句似乎是预执行的,因此与pyenv.冲突
https://stackoverflow.com/questions/50951178
复制相似问题