Python virtual environment setup not working when using pyenv-win

11 Ansichten (letzte 30 Tage)
Ben
Ben am 7 Nov. 2023
Beantwortet: Bhanu Prakash am 19 Jul. 2024
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?

Antworten (1)

Bhanu Prakash
Bhanu Prakash am 19 Jul. 2024
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!

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by