Borrar filtros
Borrar filtros

How to resolve error calling Python from MATLAB?

32 visualizaciones (últimos 30 días)
Nadatimuj
Nadatimuj el 2 de Nov. de 2022
Comentada: Siyu el 2 de Feb. de 2023
I have both MATLAB 2022a and 2022b on the same machine. MATLAB 2022a always called python without any issue. But now I installed 2022b and Python doesn't work. How to fix this error? Thanks.
Error using __init__
Python Error: TclError: Can't find a usable init.tcl in the following
directories:
C:/Users/kkkAppData/Local/Programs/Python/Python39/lib/tcl8.6
{C:/Program Files/MATLAB/R2022b/bin/lib/tcl8.6} {C:/Program
Files/MATLAB/R2022b/lib/tcl8.6} {C:/Program
Files/MATLAB/R2022b/bin/library} {C:/Program
Files/MATLAB/R2022b/library} {C:/Program
Files/MATLAB/R2022b/tcl8.6.9/library} {C:/Program
Files/MATLAB/tcl8.6.9/library}
This probably means that Tcl wasn't installed properly.

Respuestas (1)

Shoaib iqbal
Shoaib iqbal el 8 de Nov. de 2022
Looking at the Python source code Modules\_tkinter.c, TCL uses hard coded location of tcl_library_path to find its initialization files which doesn't work when Python is loaded by MATLAB.
Here is an workaround:
>> setenv('TCL_LIBRARY', 'C:\Users\sji\AppData\Local\Programs\Python\Python38\tcl\tcl8.6')
>> setenv('TK_LIBRARY', 'C:\Users\sji\AppData\Local\Programs\Python\Python38\tcl\tk8.6')
>> py.tkinter.Tk
Run these commands every time you run MATLAB. Alternatively, place the commands in a MATLAB startup script.
  3 comentarios
Nadatimuj
Nadatimuj el 23 de Nov. de 2022
@Shoaib iqbal is there no solution to this? A version that works in 2022a is broken in 2022b is very unfortunate. Can we report a bug so that they fix in 2023a? Thanks.
Siyu
Siyu el 2 de Feb. de 2023
For 2022b, there is another workaround, this error was resolved for some users by using the following code where "pyenv("ExecutionMode","OutOfProcess")" is used:
>> terminate(pyenv) % Adjusted: in case there is already a process running, we should stop it
>> pyStr = pyenv("ExecutionMode","OutOfProcess"); % Adjusted
>> pyTCL = fullfile(pyStr.Home, 'tcl', 'tcl8.6');
>> pyTK = fullfile(pyStr.Home, 'tcl', 'tk8.6');
>> setenv('TCL_LIBRARY', pyTCL);
>> setenv('TK_LIBRARY', pyTK);
The 'OutOfProcess' input in "pyenv" starts a separate process and is used for safe execution of Python scripts and libraries. Also, "terminate(pyenv)" was added to stop any "pyenv" processes which may be running. More can be read about this here: https://www.mathworks.com/help/matlab/ref/pyenv.html?searchHighlight=pyenv&s_tid=srchtitle_pyenv_1#mw_33379b1a-e442-4992-8a9a-d2e7646953a4.

Iniciar sesión para comentar.

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by