Python virtual environment in Matlab

17 vues (au cours des 30 derniers jours)
Birger Venås
Birger Venås le 14 Déc 2022
Commenté : Birger Venås le 23 Déc 2022
Hi, i tried to run Pyhon code in Matlab by using a virtual environment (venv). If i run from the virtual environment i get an error "Transport stopped"
>> pe2 = pyenv("Version","C:\pyth_test\test3\Scripts\python")
pe2 =
PythonEnvironment with properties:
Version: "3.8"
Executable: "C:\pyth_test\test3\Scripts\python.EXE"
Library: "C:\Users\birgerv\AppData\Local\Programs\Python\Python38\python38.dll"
Home: "C:\pyth_test\test3"
Status: NotLoaded
ExecutionMode: OutOfProcess
>> py.print("hello world")
Transport stopped.
if i run
>> pe = pyenv('Version','3.8',"ExecutionMode","OutOfProcess")
pe =
PythonEnvironment with properties:
Version: "3.8"
Executable: "C:\Users\birgerv\AppData\Local\Programs\Python\Python38\pythonw.exe"
Library: "C:\Users\birgerv\AppData\Local\Programs\Python\Python38\python38.dll"
Home: "C:\Users\birgerv\AppData\Local\Programs\Python\Python38"
Status: NotLoaded
ExecutionMode: OutOfProcess
>> py.print("hello world")
hello world
>>
it works. I really need to work in a virtual environment, but can not find out why it is not working.
  2 commentaires
Rajeev
Rajeev le 20 Déc 2022
Have you tried this?
pe2 = pyenv("Version", "C:\pyth_test\test3\Scripts\python", "ExecutionMode","OutOfProcess")
Birger Venås
Birger Venås le 20 Déc 2022
yes, same result. "OutOfProcess" is default i think. "InProcess" gives the same result: "Transport stopped".
pe = pyenv('Version','3.8',"ExecutionMode","OutOfProcess")
works in both "OutOfProcess" and "InProcess".

Connectez-vous pour commenter.

Réponses (1)

Rajeev
Rajeev le 21 Déc 2022
Modifié(e) : Rajeev le 21 Déc 2022
This is a known issue that the development team has been notified of.
In the meantime, I have a temporary workaround for your Python interface in MATLAB:
This workaround assumes that:
  1. The Python version installation on machine is located at C:\Users\<username>\AppData\Local\Python\python.exe
  2. The virtual environment is generated under C:\Users\<username>\venvs\myenv\
Please follow these steps to implement the workaround:
1. Set "pyenv" to the base Python installation that the "venv" is used for creating the virtual environment "myenv" in the MATLAB Command Window. For example:
>> pyenv('Version', 'C:\Users\<username>\AppData\Local\Python\python.exe')
2. Add the site-packages directory of the "myenv" virtual environment to the "py.sys.path" in the MATLAB Command Window. For example:
>> p = py.sys.path
>> p.insert(int32(0), 'C:\Users\<username>\venvs\myenv\Lib\site-packages')
The setting of "py.sys.path" must be done at the beginning of every Python Interface session and it should be set to the libraries under the "myenv" virtual environment.
  1 commentaire
Birger Venås
Birger Venås le 23 Déc 2022
Thanks! The workaround worked!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by