How to clear the Python environment on Windows

조회 수: 96 (최근 30일)
Olav Queseth
Olav Queseth 2022년 10월 4일
댓글: Olav Queseth 2022년 10월 17일
Hi!
When running Python with MATLAB there is the command
pyenv
to set or change the python interpreter that MATLAB uses.
I'm developing code that should work with and without Python installed (but with reduced functionallity) and for testing purposes I need to unset/clear the environment so that it looks like Python is not installed. I.e. so that the PythonEnvironment is not populated. Uninstalling Python does not help.
So. Is there any good way to clear the fields of the PythonEnvironment returned by pyenv ?
BTW: I'm running MATLAB on Windows 10

답변 (1개)

Vijay
Vijay 2022년 10월 14일
편집: Vijay 2022년 10월 14일
you can edit the PATH environment variable of windows and remove the path that point to python executable.
This link demonstrates the addition of python to environment. You have to remove it. You can remove it just by clearing it.
  댓글 수: 1
Olav Queseth
Olav Queseth 2022년 10월 17일
Hello @Vijay
Thanks for looking into this, but this does not answer my question. I'll try to explain a bit better:
On a clean MATLAB installation I can do:
>> pyenv
ans =
PythonEnvironment with properties:
Version: ""
Executable: ""
Library: ""
Home: ""
Status: NotLoaded
ExecutionMode: InProcess
Then I install Python on the computer and click all the defaults. The python.exe gets added to the path and then I can do:
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.7"
Executable: "C:\Users\xxxx\AppData\Local\Programs\Python\Python37\python.EXE"
Library: "C:\Users\xxxx\AppData\Local\Programs\Python\Python37\python37.dll"
Home: "C:\Users\xxxx\AppData\Local\Programs\Python\Python37"
Status: NotLoaded
ExecutionMode: InProcess
Next I uninstall Python (obviously python.exe is removed from the path)
>> pyenv
ans =
PythonEnvironment with properties:
Version: ""
Executable: ""
Library: ""
Home: ""
Status: NotLoaded
ExecutionMode: InProcess
This is all fine. But here is the problem: Suppose I installed Python in a non-standard location which is also not on the path. I can still execute Python like this:
>> pyenv('Version','C:\Python37\python.exe')
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.7"
Executable: "C:\Python37\python.exe"
Library: "C:\Python37\python37.dll"
Home: "C:\Python37"
Status: NotLoaded
ExecutionMode: InProcess
>> py.print("Hello")
Hello
But now if I uninstall Python MATLAB still thinks that Python is there, like this:
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.7"
Executable: "C:\Python37\python.exe"
Library: "C:\Python37\python37.dll"
Home: "C:\Python37"
Status: NotLoaded
ExecutionMode: InProcess
(BTW I have checked that there is no trace of Python on the path.)
The question is: Is there a way to make MATLAB forget that there was a Python interpreter installed?
The intuitive way to reset the flag does not work:
>> pyenv('Version','')
Error using pyenv
Path argument does not specify a valid executable.
I have tried looking all over for the file or registry value where the path to the Python interpreter is stored, but I'm not able to find it.
A second, but related, question is if there is a way to disable Python even if it is installed on the machine. For testing purposes it is much nicer to set a simple flag than to install/uninstall the whole interpreter to run the test.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by