nocom
Posts: 6
Joined: Thu Apr 05, 2018 7:15 pm

Where are python3 libraries searched for when using virtualenvs?

Tue Apr 10, 2018 6:49 pm

I found an article https://www.pyimagesearch.com/2016/04/1 ... -opencv-3/ on how to install opencv on Raspbian. It mentioned virtual environments and as I am a happy user of anaconda on Linux Mint I decided to install this kind of equivalence for virtual environments on my RPi. I succeeded in compiling opencv. This was put in /usr/local/lib/python3.5/dist-packages. Created a link in my virtual environment (~/.virtualenvs/ml/lib/python3.5/sitepackages). It all imports well in the "main" environment and the "ml" environment. Python3 and idle3 both import cv2 correctly.

Now I try to install the Adafruit_MotorHAT library:

Code: Select all

sudo python setup.py install
Three Adafruit "eggs" were put in the /usr/local/bin directory into which cv2 was installed. I copied these three eggs to the ml virtual environment (same as cv2). But

Code: Select all

import Adafruit_MotorHAT
in python3 returns an error message. It imports well in Idle3. As a check I deactivated the environment and then the library is correctly imported in python3.

Why is the Adafruit_MotorHAT library not imported in python3 when it is started from a virtual environment? And why are libraries not installed in the active virtual environment in the first place?

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Where are python3 libraries searched for when using virtualenvs?

Wed Apr 11, 2018 10:12 pm

Have you activated the virtual environment? Once you have, then pip will automatically install packages to it until it is deactivated.
If you want to use system packages, create the virtual environment with

Code: Select all

virtualenv --system-site-packages
rather than providing a link. Python will always look in the virtual environment first to find a package, so anything installed there by pip will over-ride the system package.

nocom
Posts: 6
Joined: Thu Apr 05, 2018 7:15 pm

Re: Where are python3 libraries searched for when using virtualenvs?

Thu Apr 12, 2018 5:53 pm

I had the virtual environment activated with workon, not in the way as you suggested. I will experiment with that and report back. Thanks a lot!

Return to “Python”