Why is my MATLAB Python Package returning the wrong current directory?

3 次查看(过去 30 天)
I am compiling the following MATLAB function into a Python Package:
function showpwd()
disp("MATLAB Time")
pwd
end
When I call showpwd in a Python script, it returns a different directory than the one that it has been called from. Furthermore, after calling this function, Python has changed its current directory. See the Python script and output below:
from __future__ import print_function
import showpwd
import matlab
import os
print(os.getcwd())
my_showpwd = showpwd.initialize()
my_showpwd.showpwd(nargout=0)
my_showpwd.terminate()
print(os.getcwd())
Output:
MATLAB Time
ans =
  'userName\Documents\MATLAB\myDir'
Before MATLAB is called: userName\Documents\MATLAB\myDir\showpwd\for_redistribution_files_only\samples 
After MATLAB is called: userName\Documents\MATLAB\myDir
Why is this happening and how can I resolve this? 

采纳的回答

MathWorks Support Team
This can occur if you have a "startup.m" file which specifies some behavior for MATLAB to execute on startup. These files can be useful when working within MATLAB, however they can cause some issues downstream if MATLAB is called from another software (in this case Python). When initializing a MATLAB generated Python package, the MATLAB process will execute the code that is provided in the "startup.m" file. So if you have some code in your "startup.m" file that changes the current directory or does something with the path, this can cause conflicts or "unexpected" behavior when trying to identify the current directory. 
To find your startup file, you can execute the following command in MATLAB:
>> which -all startup
This will identify the path to any "startup.m" file you might have created on your machine. From here, you need to:
  1. Edit the startup file (if you want it to do nothing, just comment everything out for now)
  2. Restart MATLAB
  3. Recompile your package. The changes to "startup.m" should take effect after restart.
  4. Re-edit the startup file if you still need this startup behavior in MATLAB. The next time you restart MATLAB, the original startup code will run.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Python Package Integration 的更多信息

标签

尚未输入任何标签。

产品


版本

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by