如何在 Matlab 的 virtualenv 中执行 Python 代码 [英] How can I execute Python code in a virtualenv from Matlab

查看:36
本文介绍了如何在 Matlab 的 virtualenv 中执行 Python 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用于研究的 Matlab 工具箱,我需要执行 Matlab 代码和 Python 代码.

I am creating a Matlab toolbox for research and I need to execute Matlab code but also Python code.

我想允许用户从 Matlab 执行 Python 代码.问题是,如果我马上这样做,我将不得不在 Python 的环境中安装所有东西,我想使用 virtualenv 来避免这种情况.问题是我不知道如何告诉 Matlab 用户创建的虚拟环境.

I want to allow the user to execute Python code from Matlab. The problem is that if I do it right away, I would have to install everything on the Python's environment and I want to avoid this using virtualenv. The problem is that I don't know how to tell Matlab to user the virtual enviornment created.

推荐答案

在从 MATLAB 调用 python 之前,您可以在 MATLAB 中修改 PATH 环境变量

You can either modify the PATH environment variable in MATLAB prior to calling python from MATLAB

% Modify the system PATH so it finds the python executable in your venv first
setenv('PATH', ['/path/to/my/venv/bin', pathsep, getenv('PATH')])

% Call your python script
system('python myscript.py')

或者更好的方法是指定 python 二进制文件的完整路径

Or the better way would be to specify the full path to the python binary

system('/path/to/my/venv/bin/python myscript.py')

这篇关于如何在 Matlab 的 virtualenv 中执行 Python 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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