Pyrun - error when running python script
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Marcin Kowalski
il 23 Set 2022
Commentato: Bala Tripura Bodapati
il 28 Set 2022
Hi,
I am trying to run a python script from MATLAB. The script aims to setup a websocket server.
>> pyrun('webs_server.py')
Error using <string>><module> (line 1)
Python Error: NameError: name 'webs_server' is not defined
Python script runs fine when run under Conda.
My pyenv is following:
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "C:\Users\kowal\AppData\Local\Programs\Python\Python39\python.EXE"
Library: "C:\Users\kowal\AppData\Local\Programs\Python\Python39\python39.dll"
Home: "C:\Users\kowal\AppData\Local\Programs\Python\Python39"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "15984"
ProcessName: "MATLAB"
The directory with the script is in the path. What could be the possible issue?
0 Commenti
Risposta accettata
Bala Tripura Bodapati
il 26 Set 2022
Hi Marcin
It is my understanding that you would like to execute Python script from MATLAB and are encountering an error while using the 'pyrun' function.
The 'pyrun' function is used to execute Python commands from MATLAB. To execute a Python script file, the 'pyrunfile' function is used. The filename of the code snippet should be passed as an argument to the 'pyrunfile' function. The following code illustrates the usage of the 'pyrunfile' function:
>>pyrunfile('webs_server.py')
2 Commenti
Bala Tripura Bodapati
il 28 Set 2022
Hi Marcin,
It is my understanding that 'ModuleNotFoundError' error is encountered.In order to verify the libraries that are visible to MATLAB, ensure that the libraries are there in the Python sys.path :
py.sys.path
If not, you can install the libraries in the Python system path.
This issue can also be encounterd if more than 2 versions of Python are installed on your system.The right version can be configured using the following command:
pyenv('Version', 'C:\ProgramData\Anaconda3\python.exe')
Please refer below documentation for more information:
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Call Python from MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!