简体   繁体   English

如何从 MATLAB 在 Conda 环境中运行 Python 脚本?

[英]How to run a Python script in a Conda environment from MATLAB?

I am writing a package to give to someone else so I am trying to make installation, dependencies, and use as simple as possible.我正在写一个 package 给别人,所以我试图使安装、依赖和使用尽可能简单。 The top-level calls for the package are in MATLAB but, at one point, I'd like to run a Python script from my MATLAB function. The top-level calls for the package are in MATLAB but, at one point, I'd like to run a Python script from my MATLAB function. I don't know if the user will be on Unix or Windows.我不知道用户是否会在 Unix 或 Windows 上。 So far, I have used a Conda environment for my Python scripts because it uses specific versions of Scipy and Tensorflow.到目前为止,我已经为我的 Python 脚本使用了 Conda 环境,因为它使用特定版本的 Scipy 和 Tensorflow。

Is there a platform-independent way to activate a Conda environment and call a Python script from MATLAB?是否有独立于平台的方法来激活 Conda 环境并从 MATLAB 调用 Python 脚本? Or even better, have a copy of Python (and it's dependencies) in a subdirectory of the MATLAB script so I could, for instance, give the entire package on a USB stick and not worry about different Python installations? Or even better, have a copy of Python (and it's dependencies) in a subdirectory of the MATLAB script so I could, for instance, give the entire package on a USB stick and not worry about different Python installations?

Thanks for any insight.感谢您的任何见解。

There is not a platform-independent method to make MATLAB use a conda environment.没有一种独立于平台的方法可以让 MATLAB 使用conda环境。 However, as I mentioned in this answer, you can explicitly tell MATLAB where to look for the Python interpreter and libraries of your environment, and can even de-bug without having to restart MATLAB every time you change the Python script you are trying to run. However, as I mentioned in this answer, you can explicitly tell MATLAB where to look for the Python interpreter and libraries of your environment, and can even de-bug without having to restart MATLAB every time you change the Python script you are trying to run .

You do not need to activate the conda environment before using it from MATLAB, so that part of your question is moot.在从 MATLAB 使用conda环境之前,您不需要激活它,因此您的部分问题没有实际意义。 MATLAB simply uses the interpreter, so as long as all of the necessary libraries are installed in the conda environment, your Python code will run from MATLAB. MATLAB 只是使用解释器,因此只要在conda环境中安装了所有必需的库,您的 Python 代码将从 MATLAB 运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM