How to use a pyenv-win virtual environment with Python Interface
13 views (last 30 days)
Show older comments
MathWorks Support Team
on 9 Oct 2023
Answered: MathWorks Support Team
on 9 Oct 2023
I am using pyenv-win to manage Python projects that I will use in R2019b. I used the "pyenv" command to direct MATLAB to use the Python interpreter associated with my pyenv-win virtual environment, but I am getting an error when I try to run any Python commands from MATLAB.
>> pyenv('Version','C:\Users\username\.pyenv\pyenv-win\prj\test_prj\env\Scripts\python.exe')
ans =
PythonEnvironment with properties:
Version: "3.7"
Executable: "C:\Users\username\.pyenv\pyenv-win\prj\test_prj\env\Scripts\python.exe"
Library: "C:\Users\username\.pyenv\pyenv-win\shims\python37"
Home: "C:\Users\username\.pyenv\pyenv-win\prj\test_prj\env"
Status: NotLoaded
ExecutionMode: OutOfProcess
>> p = py.list
Unable to resolve the name py.list
How can I use a pyenv-win virtual environment with Python Interface?
Accepted Answer
MathWorks Support Team
on 9 Oct 2023
The issue that you encountered is due to MATLAB not finding the correct location of the Python library. This issue only occurs in releases R2021a, and earlier.
When pyenv-win is installed, it adds two paths to the User PATH environment variable.
C:\Users\username\.pyenv\pyenv-win\bin
C:\Users\username\.pyenv\pyenv-win\shims
MATLAB incorrectly identifies the pyenv-win "shims" folder as the location of the Python library (see the "Library" property in the 'pyenv" output, above). To fix this,
1) Close MATLAB.
2) Add the location of the Python library to the User PATH environment variable ahead of the "shims" folder. In this example, the Python library is located in the "C:\Users\gkepler\.pyenv\pyenv-win\versions\3.7.9" folder, so the User PATH information should contain these paths in this order.
C:\Users\username\.pyenv\pyenv-win\bin
C:\Users\gkepler\.pyenv\pyenv-win\versions\3.7.9\
C:\Users\username\.pyenv\pyenv-win\shims
3) Restart MATLAB and use "pyenv" to specify the Python interpreter associated with your pyenv-win virtual environment.
>> pyenv('Version','C:\Users\username\.pyenv\pyenv-win\prj\test_prj\env\Scripts\python.exe')
ans =
PythonEnvironment with properties:
Version: "3.7"
Executable: "C:\Users\username\.pyenv\pyenv-win\prj\test_prj\env\Scripts\python.exe"
Library: " C:\Users\gkepler\.pyenv\pyenv-win\versions\3.7.9\"
Home: "C:\Users\username\.pyenv\pyenv-win\prj\test_prj\env"
Status: NotLoaded
ExecutionMode: OutOfProcess
>> py.list
ans =
Python list with no properties.
[]
0 Comments
More Answers (0)
See Also
Categories
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!