eBear
eBear

Reputation: 31

is "which" a function in matlab?

I'm trying to use 'which python3' in MATLAB's system command, and it doesn't work. The goal here is to write a script that can run a specific version of python without hard-coding the path into the script. I'd like the script to find python3 and then call it by the full pathname.

When I run the following command in MATLAB,

>> [stat, result] = system('which python3')

I get the following output:

stat =

 1

result =

 0x0 empty char array

However, I can run the following command in UNIX and get a good result:

$ which python3
/usr/local/bin/python3

Additionally, this works fine in MATLAB:

>> [stat, result] = system('which python')
stat =

 0

result =

'/usr/bin/python
'

and in UNIX

$ which python
/usr/bin/python

Upvotes: 2

Views: 74

Answers (1)

eBear
eBear

Reputation: 31

I got some help on this one from the "MATLAB Answers" forum. User Walter Robertson told me

MATLAB typically is launched as a graphics program by the operating systems. Such programs do not execute the user's login scripts, so environment variables are not set to user login values: they stay at the system defaults.

system() does not run scripts in a "login" context, so profile variables are not set up either.

The only thing that executes are the scripts that are run every time a shell is initialized.

Thus, my login script wasn't run, so the path to Python 3 wasn't included in the context from which MATLAB's system runs the which command, so I get an empty result from system.

Upvotes: 1

Related Questions