Python virtual environment setup not working when using pyenv-win

조회 수: 15 (최근 30일)
Ben
Ben 2023년 11월 7일
답변: Bhanu Prakash 2024년 7월 19일
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