tony1812
Posts: 354
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

idle in virtual environment

Thu Nov 09, 2017 3:39 pm

Hello, I am running opencv in a environment, in the terminal, I just type source ~.profile than work cv than run the script it works all fine. However, when I try to edit the script by using the stock IDEL3 editor, the IDLE3 editor reports that import cv2 module not found. I suppose the global copy of IDLE can't find the opencv in the environment? Do I need to install a copy of the IDLE within the environment? Thanks.

scotty101
Posts: 3670
Joined: Fri Jun 08, 2012 6:03 pm

Re: idle in virtual environment

Thu Nov 09, 2017 4:10 pm

The command line with your virtual environment, try running

Code: Select all

python -c "from idlelib.PyShell import main; main()"
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

tony1812
Posts: 354
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Re: idle in virtual environment

Thu Nov 09, 2017 6:17 pm

Thanks for the reply.

A similar question is about the GPIOs, when my codes run in the environment (cv)..... It also says No module named RPi. I read on SO, someone says I need to install RPi.GPIO Within the environment, so I did pip3 install RPi.GPIO I get 'Requirement already satisfied...' I am not sure if I fully understand the concept of virtualenv. Shouldn't the RPi covers globally?

il_diavolo
Posts: 137
Joined: Mon Dec 02, 2013 7:56 pm

Re: idle in virtual environment

Thu Nov 09, 2017 10:45 pm

I guess you have followed the recipe in PyImageSearch? I have just re-compiled OpenCV 3.3.0 in a new Stretch installation using those instructions but omitting the virtual environment steps. This means that OpenCV is installed in the normal Pi environment where it is accessible by other applications, including Idle. I did this after finding the same problems you are experiencing.
By the way, it took over 8 hours to compile on my RPI 3B.

scotty101
Posts: 3670
Joined: Fri Jun 08, 2012 6:03 pm

Re: idle in virtual environment

Fri Nov 10, 2017 9:01 am

il_diavolo wrote:
Thu Nov 09, 2017 10:45 pm
By the way, it took over 8 hours to compile on my RPI 3B.
You should have a read of this blog post https://www.raspberrypi.org/blog/piwheels/. Should speed things up for you.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

tony1812
Posts: 354
Joined: Sat Jul 15, 2017 12:40 pm
Location: Boston MA.

Re: idle in virtual environment

Fri Nov 10, 2017 11:59 am

Thanks for all the replies.
Yes, I did followed PyImageSearch. It took me a while to compile opencv as well. Adrian has so many articles on this subject, I found this most helpful-- https://www.pyimagesearch.com/2017/10/0 ... pberry-pi/ with the size of swapfile increased.
with make -j4, it took me under two hours from start to end on pi3. :)
And yes, I did try to omit the virtual environment step, compiled it under the system python. But it does have a advantage in using virtual environment tho, in case I screwed up, I don't screw up the whole system. :)
BTW, I was also misinformed by SO, It says to use pip3 install RPi.GPIO, actually, I used pip install RPi.GPIO in the virtual environment than it works.
However I am not very clear on this, isn't pip is only for python 2 and pip3 is for python3?
Last edited by tony1812 on Fri Nov 10, 2017 1:15 pm, edited 1 time in total.

User avatar
croston
Posts: 702
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: idle in virtual environment

Fri Nov 10, 2017 12:43 pm

tony1812 wrote:
Fri Nov 10, 2017 11:59 am
BTW, I was also misinformed by SO, It says to use pip3 install RPi.GPIO, actually, I used pip install RPi.GPIO in the virtual environment than it works.
However I am not very clear on this, isn't pip is only for python 2 and pip3 is for python3?
When creating a python virtual environment, you are setting up an area with known versions of python and modules for your project. This is used so that different projects with different versions of dependencies don't conflict with each other. When you are in a virtual environment created with Python 3, then the python and pip commands work with the versions of python and pip you have created to run within your virtual environment.

You can see what is going on by using the commands:

Code: Select all

$ which python
$ which pip
Do this both inside and outside your venv to see the difference.

Return to “Python”