Pyrun - error when running python script
23 ビュー (過去 30 日間)
古いコメントを表示
Marcin Kowalski
2022 年 9 月 23 日
コメント済み: Bala Tripura Bodapati
2022 年 9 月 28 日
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!