I am actually creating my project wherein I can control my raspberry pi in my pc using FPV Radio Telemetry. The scenario is my pc will send string data like "red" and "blue" to my pi. After my pi received the string, it will detect an object with either color red or blue. I used OpenCV to do it and it's already working. The problem is when I used import cv2 and import serial in same python file and when I run the file like python example.py module cv2 is running but module serial is not found and when I run sudo python example.py module serial will run but cv2 is not found. How can I run both of them in a single python file? I am actually using python 3.5.3, open cv version 3.3.0.

Here are the scripts For UART

import time
import serial
import os
counter =0;
while 1:
     os.system("python example-color.py {0}". format(x)) 

Scripts using opencv

    from picamera.array import PiRGBArray
    from picamera import PiCamera
    import time
    import cv2
    import numpy as np
    import sys

    camera = PiCamera()
    camera.resolution = (640, 480)
    camera.brightness = 50
    camera.framerate = 32
    rawCapture = PiRGBArray(camera, size=(640, 480))
    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):

        image = frame.array
        cv2.imshow("Frame", image)
        key = cv2.waitKey(1) & 0xFF
        hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        if sys.argv=="red":
        elif sys.argv=="blue":
        elif sys.argv=="green":
        elif sys.argv=="yellow":
  • You can first try something like this: (1) Suppose you have a funtion a1() in program/module A.py, and another function b1() in program/module B.py, (2) Now use program C.py to first "import A" and "import B", (3) then in C.py run a1() as "A.a1()", and run b1() as "B.b1()". Of course there are alternatives, such as running both functions a1() and b1 () in B.py say, by first (4) "import A" into B.py, then (5) in B.py, run the two functions as "a1()" and "b1()". – tlfong01 Apr 17 '20 at 4:22
  • What I did is I have A.py it contains the "import serial" on it, and "example-color.py" where I put my import cv2. I use this code < os.system("python example-color.py {0}". format(X)) > inside A.py and when I run "sudo python A.py" still the error is : No module named cv2.. – user3427379 Apr 17 '20 at 5:07
  • Of course this works. I am not going to guess at what you have done wrong. If you want help post a tiny script which exhibits the problem. – joan Apr 17 '20 at 7:42
  • Ah, now let me see. So you have two python programs: (1) A.py with a "import serial" statement, (2) example-color-py with an "import cv2" statement. (3) A.py has another statement "os.system("python example-color.py {0}", (4) Now when you run "sudo python A.py", you get the "No module named cv2" error message. Do I understand your problem correctly? – tlfong01 Apr 17 '20 at 8:19
  • Anyway, do NOT use os.system(...). Try C to run A and B's functions. Cheers. – tlfong01 Apr 17 '20 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.