How to clear the Python environment on Windows

93 次查看(过去 30 天)
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