MATLAB无法通过导入调用Python模块 [英] MATLAB can't call Python module with imports

查看:295
本文介绍了MATLAB无法通过导入调用Python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照上的说明进行操作此页面,以使MATLAB在python模块中调用python代码.

I've followed the instructions on this page to have MATLAB call python code in a python module.

# mymod.py

def search(words):
    """Return list of words containing 'son'"""
    newlist = [w for w in words if 'son' in w]
    return newlist

然后在MATLAB中运行

Then in MATLAB I run

N = py.list({'Jones','Johnson','James'})
py.mymod.search(N)

并获得所需的输出.但是,当python模块本身具有导入功能时,我无法正常工作.例如,将单行import numpy as np添加到mymod.py会产生错误

and get the desired output. However, I can't get things to work when the python module itself has imports in it. For example, adding the single line import numpy as np to mymod.py yields the error

Undefined variable "py" or class "py.mymod.search"

在MATLAB中运行相同的代码行.我该如何解决?

upon running the same lines in MATLAB. How can I get around this?

推荐答案

matlab 在第110页的书中,您可以看到看到此错误的原因以及如何对它们进行故障排除的可能原因.

In the matlab book on page 110 you can see the list of possible reasons for why you saw this error and how to troubleshoot them.

原因:

  1. 第1-70页上的未安装Python"
  2. 第1-70页的"Windows平台上的Python的64位/32位版本"
  3. 第1-71页上的"MATLAB无法找到Python"
  4. 第1-71页上的用户定义的Python模块中的错误"
  5. 第1-71页上的"Python模块不在Python搜索路径上"
  6. 第1-72页上的模块名称冲突"
  7. 第1-72页的"Python尝试在错误的模块中执行命令"

我建议您仔细阅读这份解决方案列表,因为您会发现错误的几种可能原因,并且如果没有更多信息,我们很难知道这是哪一个.

I would suggest working through this list of solutions as you can see there are several possible reasons for your error and it's hard for us to know which one it is without more information.

这篇关于MATLAB无法通过导入调用Python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆