2

I am writing a package to give to someone else so I am trying to make installation, dependencies, and use as simple as possible. 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. So far, I have used a Conda environment for my Python scripts because it uses specific versions of Scipy and Tensorflow.

Is there a platform-independent way to activate a Conda environment and call a Python script from MATLAB? 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.

jjohnson28
  • 43
  • 2
  • You can likely use `conda run` similar to [what is done here from Node.js](https://stackoverflow.com/questions/58456098/python-package-managers-and-node-js/58458618#58458618). In *nix, [one can put that directly in the script's shebang](https://stackoverflow.com/a/58455069/570918), but I'm not sure if there is a Windows equivalent for that. – merv Nov 15 '19 at 16:02
  • @merv it is not necessary to activate the conda environment, only to have everything installed in it prior to running from MATLAB, as I explain below: https://stackoverflow.com/a/63671567/12763497 – brethvoice Aug 31 '20 at 13:19

1 Answers1

0

There is not a platform-independent method to make MATLAB use a conda environment. 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 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.

brethvoice
  • 350
  • 1
  • 4
  • 14