Python virtual environment setup not working when using pyenv-win

11 次查看(过去 30 天)
Using MATLAB 2021a, I am trying to interface with Python code. I used pyenv-win to install Python 3.8.10 on my Windows computer. I then created a virtual environment using the following command in PowerShell:
"C:\Users\me\.pyenv\pyenv-win\shims\python3.8.bat" -m venv "C:\Users\me\Documents\my-matlab-project\pyVenv"
I then set MATLAB to use this virtual environment by running the following in MATLAB:
pyenv("Version",fullfile("C:\Users\me\Documents\my-matlab-project\pyVenv","Scripts","python"),"ExecutionMode","OutOfProcess");
When I pressed the return key, I saw a Python REPL window appear briefly and then disappear. I then tried to run the following:
py.print("Test");
Several Python windows appeared and disappeared rapidly, and after several seconds, I received the error Unable to resolve the name py.print.
When I ran
pyenv()
in MATLAB, I noticed that the Python library path does not appear to be correct. I am not sure if this is the problem:
ans =
PythonEnvironment with properties:
Version: "3.8"
Executable: "C:\Users\zelnickb\OneDrive - Children's Hospital of Philadelphia\Documents\VCS Repositories\Luminesce\py-venv\Scripts\python.EXE"
Library: "C:\Users\zelnickb\.pyenv\pyenv-win\shims\python38"
Home: "C:\Users\zelnickb\OneDrive - Children's Hospital of Philadelphia\Documents\VCS Repositories\Luminesce\py-venv"
Status: NotLoaded
ExecutionMode: OutOfProcess
How can I correct this problem so that I can run Python code from MATLAB in my virtual environment?

回答(1 个)

Bhanu Prakash
Bhanu Prakash 2024-7-19
Hi Ben,
This issue might occur when MATLAB does not find the correct path to the Python library. It can be observed in MATLAB R2021a (the release you are using) and earlier releases.
To resolve the issue and use the pyenv-win virtual environment, you can follow the steps mentioned in the following MATLAB answer posted by the MathWorks Support Team:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by