0

A python script that uses cv2 runs fine from command line once the virtual environment is activated ((venv) pi@raspberrypi:~/test $ python3 openCV_motion_detection_cam1.py ). But when run from normal command line (pi@raspberrypi:~/test $ python3 openCV_motion_detection_cam1.py ) , it outputs error - "No module named 'cv2'", which is already inside /home/pi/test/venv/lib/python3.7/site-packages.I am a newbie and expecting your comments to be 'Noob' friendly . Please help.

2
  • 1
    That's the concept of virtual environments: you activate them to work. Commented Oct 8, 2021 at 17:30
  • Your script runs fine, then what do you need help with? Commented Oct 8, 2021 at 17:33

2 Answers 2

2

If you want to directly run a script in a virtual env, edit the shebang line to include the Python interpreter from that env:

#!/path/to/env/bin/python3

That interpreter will find the pyvenv.cfg file at its parent directory and will adjust all paths accordingly. That is an equivalent of activating the environmnet.


The process can be automated. Create a package with a setup.py and declare which files are scripts. When the package is installed, the scripts will be installed in the <venv>/bin subdirectory with correct shebang lines.

You may want to create a symlink from /usr/bin to have the script in the path.

Sign up to request clarification or add additional context in comments.

4 Comments

This is the shebang line I used '#!/home/pi/test/venv/bin/python3' with no luck. Regarding creating package and Symlink, I still need to google how. Thank you
@technocraze That's strange, the shebang line is prooved to work. I'm using it too. Details are here: docs.python.org/3/library/site.html in the paragraph: If a file named “pyvenv.cfg” exists ... (no need to read the rest)
@technocraze How do you start your script? You have to make it executable (chmod 755 ...) and run directly /path/to/script, NOT with python3 /path/to/script or else the shebang line is not evaluated by the shell.
I made the script executable and it did the trick, the script runs fine from command line now! Actually I already tried making the script executable but did not succeed, I think the shebang line was not correct then. Thank you very much VPfB. Thanks to all for your time and efforts.
1

Virtual environments exist to isolate the modules needed for individual python programs from each other. When you installed cv2 you installed it within your currently active venv. When the venv is not active it's modules will not be available on. Simple solution is to run your program within the venv.

You can tell that the module exists within a venv by the /venv/lib/python37/site-packages portion of the modules path.

1 Comment

Thank you for the response. The script is running within the venv once activated. I need to start this script on boot and hence want to add to crontab or rc. local. To achieve this I think the script should run from the command prompt. Could you please let me know how to tell where the module exists? I am a newbie and have very limited knowledge in defining paths etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.