Pyrun - error when running python script
25 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Bala Tripura Bodapati
2022-9-26
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 个评论
Bala Tripura Bodapati
2022-9-28
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:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!