Errors calling python scripts in virtual environment on raspberry pi (MCC Daqhat Library) - with a possible answer

8 次查看(过去 30 天)
I'm posting this mostly as help to someone else trying to develop a system using MCC's DAQHATS. There are very little resources regarding using Daqhats from MCC from Matlab (unsupported) or MCC.
The installation of the DAQHAT python library requires that it be installed into a virtual environment. That was causing us issues expecially when we need to run 2 scripts back to back. It would close the virtual environment after the first script.
The following is the question we submitted to Matlab's help team:
Description: We are using MCC Daqhats. First, I know this is not a supported hardware for Matlab. We are not asking for support for that. However, in the process of installing the python libraries for the Daqhats, we are forced to install them into a virtual environment. We believe this is causing us problems. We need to run multiple scripts in a row. We think that after the first call it is exiting the virtual environment but we are not certain of that.
The matlab file calling the python script is attached. The python script we are calling is attached. We have tested the python script on the PI and it runs properly both from python and the command line.
here is our Matlab error:
Error using datacollection
Error executing command "python3 /home/aset/daqhats/examples/python/mcc172/bert2.py". Details:
STDERR: Traceback (most recent call last):
File "/home/aset/daqhats/examples/python/mcc172/bert2.py", line 3, in <module>
from daqhats import mcc172, OptionFlags, SourceType, HatIDs, HatError
ModuleNotFoundError: No module named 'daqhats'
Our Solution was as follows: (However others may have a better solution)
We have been able to get it to work by explicitly calling the virtual environment in the system command. This was found using a resource other than a Matlab site.
system(rpi, '/home/aset/environments/bin/python3 /home/aset/daqhats/examples/python/mcc172/bert2.py')
  1 个评论
Umar
Umar 2024-8-16

Hi @Paul Elliott,

I don’t know if it’s coincidence that we interact with each other sharing our technical thoughts. I always learn a lot when responding to your technical issues. This time, let me address your query regarding, “However others may have a better solution”

First, let me address topic regarding understanding virtual environments, When you activate a virtual environment, your shell’s context changes to use this isolated environment. The key challenge here is that when you run a script from MATLAB using `system()`, it may not maintain the context of the activated virtual environment unless explicitly defined. Based on your description, it seems that when you run your first Python script from MATLAB, it successfully accesses the “daqhats” module because it's being executed in an activated virtual environment. However, subsequent calls do not maintain this context, leading to the “ModuleNotFoundError”. This is a common issue when executing scripts from environments that require specific dependencies or configurations.

Now, your proposed solution of explicitly calling the Python interpreter from within the virtual environment in your “system()” command is indeed a valid approach. By specifying the full path to the Python executable in your virtual environment, I would sugest when each time you call your script, it runs in the correct context.Here is an example based on your solution:

% Define the path to your virtual environment's Python executable
python_executable = '/home/aset/environments/bin/python3';
% Define the path to your script
script_path = '/home/aset/daqhats/examples/python/mcc172/bert2.py';
% Call the script using system command
[status, cmdout] = system([python_executable ' ' script_path]);
% Check for errors
if status ~= 0
    disp('Error executing script:');
    disp(cmdout);
else
    disp('Script executed successfully.');
end

So, the code snippet that I provided will capture both execution and error handling, providing feedback based on whether the script ran successfully or encountered issues. For future reference, I will suggest if you need to run multiple scripts back-to-back frequently, consider creating a wrapper Python script that sequentially calls each required function or module. This way, you only need to activate your virtual environment once at the start and for For complex projects involving multiple dependencies or libraries, consider using tools like `pipenv` or `conda`, which can help manage environments and dependencies more efficiently.

Since you mentioned limited resources for MCC DAQHATS with MATLAB, consider reaching out to community forums or groups focused on MATLAB-Python integration for troubleshooting specific errors encountered during execution, so when you are troubleshooting these kind of technical problems on your end, the technical team at Mathworks can work in parallel to resolve these issues which will help you not falling behind on your project. I just wanted to share my two cents. Hope this helps.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by